Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

category term has "object_slug" null on endpoint #42

Open
tomas-javurek opened this issue Apr 22, 2017 · 4 comments
Open

category term has "object_slug" null on endpoint #42

tomas-javurek opened this issue Apr 22, 2017 · 4 comments

Comments

@tomas-javurek
Copy link

category term has "object_slug" null on endpoint
page has "object_slug" defined properly

@aunruh
Copy link

aunruh commented Sep 18, 2017

Hey this function format_menu_item just gets the slug for posts/pages but not for terms. I changed the function to get it for terms as well. @metazoa-org I hope you can include this in an update.

        public function format_menu_item( $menu_item, $children = false, $menu = array() ) {

            $item = (array) $menu_item;

            $object_slug = '';
            if($item['type'] == 'taxonomy'){
            	$term = get_term($item['object_id'], $item['object']);
            	$object_slug = $term->slug;
            }else{
            	$object_slug = get_post( $item['object_id'] )->post_name;
            }
            
            $menu_item = array(
                'id'          => abs( $item['ID'] ),
                'order'       => (int) $item['menu_order'],
                'parent'      => abs( $item['menu_item_parent'] ),
                'title'       => $item['title'],
                'url'         => $item['url'],
                'attr'        => $item['attr_title'],
                'target'      => $item['target'],
                'classes'     => implode( ' ', $item['classes'] ),
                'xfn'         => $item['xfn'],
                'description' => $item['description'],
                'object_id'   => abs( $item['object_id'] ),
                'object'      => $item['object'],
                'object_slug' => $object_slug,
                'type'        => $item['type'],
                'type_label'  => $item['type_label'],
            );

            if ( $children === true && ! empty( $menu ) ) {
	            $menu_item['children'] = $this->get_nav_menu_item_children( $item['ID'], $menu );
            }

            return apply_filters( 'rest_menus_format_menu_item', $menu_item );
        }

@aunruh
Copy link

aunruh commented Sep 18, 2017

Better yet, use a filter to add the slug to a category menu point:

<?php 
// fix for: https://github.com/unfulvio/wp-api-menus/issues/42

add_filter('rest_menus_format_menu_item', function($item){
    if($item['type'] == 'taxonomy'){
        $term = get_term($item['object_id'], $item['object']);
        $item['object_slug'] = $term->slug;
    }
    return $item;
}, 10, 1);

@adrienrn
Copy link

Hi,

I have a similar issue with a menu item where the type is post_type_archive. This is a page from a plugin.

It triggers a notice at the top of the response which makes it "invalid" for the consumer:

< br / > < b > Notice < /b>:  Trying to get property of non-object in <b>/[...]/wp-content/plugins/wp-api-menus/includes/wp-api-menus-v2.php < /b> on line <b>386</b > < br / > {
    "ID": 2,
    "name": "Main",
    "slug": "main",
[...]

I think this is related to this issue because the code assume that get_post( $item['object_id'] ) will return something whereas get_post can an will return null if the post is not found for some reason.

I've made a hotfix branch on my fork here but I believe that it can be improved before making a pull request here.

What are all the type that can be post_type, custom, taxonomy ? Let's see if we can handle them all ⚗️ !

PS: The given filter does not work because it's done after the item has been formatted and the notice is triggered/added no matter what 😞 .

@aurovrata
Copy link

both post archive and taxonomy terms are fixed in this repo. I raised a PR but i feel the author has abandoned this plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants