• Resolved gad123

    (@gad123)


    Hi,
    For some reason the following line

    $terms = wp_get_post_terms($p->ID, ‘item_year’);

    returns WP_Error.
    According to https://developer.www.ads-software.com/reference/functions/wp_get_post_terms/ it means that no terms were found.
    ‘item_year’ is the name of the taxonomy I created and each post has a value for it.
    $p represents post id from type: post.
    When changing ‘item_year’ to ‘post_tag’ and adding echo statement to show the id and first tag of each post, I got the correct results so I believe it shows that my loop is correct and the problem is with the taxonomy ‘item_year’…
    Any help will be much appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Just to be certain, the post ID in question is of a post type that this item_year taxonomy is associated with, correct? Just a detail wanting to make sure of.

    Beyond that, I’m not sure what may be going on with that, and why it’s not finding anything, unless you don’t have any posts published for it, or the taxonomy doesn’t have any terms assigned to it. Both of which I’m assuming are not the case.

    Also curious if this function may achieve what you’re looking for as well. Bringing it up because the WP Developer page for your used function mentions that it’s not cached, while get_the_terms() is

    https://developer.www.ads-software.com/reference/functions/get_the_terms/

    Thread Starter gad123

    (@gad123)

    Thanks Micahel for your answer.
    I replaced wp_get_post_terms by get_the_terms but got the same results (correct results for post_tag, WP_Error for ‘item_year’.
    Since I’m new to this plugin I would appreciate if you could go over my explanation and tell me if I ‘ve done something wrong. Here’s how ‘item_year_ is defined:

    function cptui_register_my_taxes_item_year() {

    /**
    * Taxonomy: item_year.
    */

    $labels = [
    “name” => __( “item_year”, “custom-post-type-ui” ),
    “singular_name” => __( “item_year”, “custom-post-type-ui” ),
    ];

    $args = [
    “label” => __( “item_year”, “custom-post-type-ui” ),
    “labels” => $labels,
    “public” => true,
    “publicly_queryable” => true,
    “hierarchical” => false,
    “show_ui” => true,
    “show_in_menu” => true,
    “show_in_nav_menus” => true,
    “query_var” => true,
    “rewrite” => [ ‘slug’ => ‘item_year’, ‘with_front’ => true, ],
    “show_admin_column” => true,
    “show_in_rest” => true,
    “show_tagcloud” => false,
    “rest_base” => “item_year”,
    “rest_controller_class” => “WP_REST_Terms_Controller”,
    “show_in_quick_edit” => false,
    “show_in_graphql” => false,
    ];
    register_taxonomy( “item_year”, [ “post” ], $args );
    }
    add_action( ‘init’, ‘cptui_register_my_taxes_item_year’ );

    for each post I choose “Edit”, saw on the sidebar (below tags and categories), “item_year” and under “Add new item_year” I entered a value (for example, 2020).

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Based on the Developer page:

    (array|WP_Error) Array of WP_Term objects on success or empty array if no terms were found. WP_Error object if $taxonomy doesn’t exist.

    If you’re receiving an error, that means the taxonomy hasn’t been registered yet. If there’s no terms, it just returns an empty array.

    So that has me wondering where you’re trying to access the post information at. Is it in a custom spot in some way? or is it trying to show them on the frontend for general visitors?

    As is, in the header.php file for example, I haven’t encountered any issues, but that’ll definitely be well after taxonomies get registered.

    Thread Starter gad123

    (@gad123)

    I’m using List Custom Taxonomy Widget to display the taxanomy terms, see for example the right sidebar of https://marianna.ruahmidbar.com/en/blog/
    “Subjects” are WP native categories, but all the rest are taxonomies.

    • This reply was modified 2 years, 10 months ago by gad123.
    Thread Starter gad123

    (@gad123)

    Maybe I should run this code from somewhere else. Do you believe that there might be a difference if I run it from a different php file?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    only way a different PHP file would really affect anything would be if the file was loaded at a potentially later time.

    The WordPress hooks system would be more important here. Taxonomies are registered on init hook. With CPTUI, we register on priority 8-9 or so, as opposed to the default 10. So any code being run after init priority 9 should have the taxonomies in question registered by then.

    Thread Starter gad123

    (@gad123)

    YYYYYYYYYEEEEEEEEEEEEEEESSSSSSSSSSSSSSS !!!!! ??
    Moving the code to header.php solved this issue!
    Thanks Michael for this great explanation!
    Much appreciated!

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wp_get_post_terms returns WP_Error’ is closed to new replies.