Support » Fixing WordPress » How to show terms of a custom post type category?

  • jasghar

    (@jasghar)


    I have custom categories of custom post type and inside custom categories, they have terms set up. i want to get the terms of the custom category ‘Model’
    how can I get it and then show custom posts type related to ‘model’ term

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can get terms related to any post type in any taxonomy with wp_get_post_categories(). If the taxonomy is not “category”, use the “taxonomy” arg to specify what you want. This returns an array of term objects. With it you can compose appropriate args to get related posts with get_posts().

    wp_get_post_categories() is basically a wrapper function of wp_get_object_terms(), which you could use for the same purpose.

    Thread Starter jasghar

    (@jasghar)

    I’m not actually getting that, can you explain a little more, please
    $post_categories = wp_get_post_categories($postid);

    but I’m confused about why we need to mention the post id in it.

    can you please write the code which will get the terms

    my terms are here
    listing post type > listing category named ‘interests’ > terms

    thanks

    Moderator bcworkz

    (@bcworkz)

    Oh, you want all terms in a taxonomy regardless of which post they’re assigned to? Then yeah, the post ID, or the post type in general have nothing to do with it. You can use get_terms() to do that, specifying your taxonomy slug as the “taxonomy” arg.
    https://developer.wordpress.org/reference/functions/get_terms/

    You can also constrain the query in many other ways if desired, such as only getting terms with a particular meta value. Possible args are listed here.

    Thread Starter jasghar

    (@jasghar)

    buddy thanks for your help but i’m getting an error
    if I write this
    $terms = get_terms( ‘post_tag’, array(
    ‘hide_empty’ => false,
    ) );

    then it will show the tag terms which are under wp default post tab
    dashboard > post > tag

    and if I write something like this
    $terms = get_terms( array(
    ‘taxonomy’ => ‘interests’,
    ‘hide_empty’ => false,
    ) );

    then it returns an error
    wp_error_object

    I want to show the terms which are in the interests category so I type interests next to taxonomy
    but its not working

    here is the URL of the terms inside interests category
    https://valueautosdurango.com/wp-admin/edit.php?post_type=listings&page=interests

    see no taxonomy word in URL

    Dont know where I’m doing the issue

    Moderator bcworkz

    (@bcworkz)

    terms which are in the interests category

    Do you mean interests taxonomy? “category” is a different taxonomy. Terms of different taxonomies don’t normally directly relate to each other, they only relate through posts they are assigned to.

    That’s a very strange URL for a terms list table. edit.php is for post types, not taxonomy terms. Taxonomy terms are listed by edit-tags.php. And as you say, no mention of taxonomy in the query string. I don’t know what a page query string is for in this context.

    A list table URL for a custom taxonomy should be more like wp-admin/edit-tags.php?taxonomy=interests&post_type=listings

    how can I get it and then show custom posts type related to ‘model’ term

    If your intention is to get posts of type “listings” assigned the interests term “model”, use get_posts() and specify the taxonomy constraints of the query with the “tax_query” arg.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.