• Resolved Harryson

    (@harryson)


    So my custom post type uses categories and tags. How can I get the list of all the categories used in that post type dynamically and display them on the front-end?

    And also, how can I easily generate tag cloud based on the custom post types tags?

    Harryson

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    For your first question (categories) try it with this:

    <?php
      global $wpdb;
      $post_type_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'my_custom_post_type' AND post_status = 'publish'");
      if($post_type_ids){
        $post_type_cats = wp_get_object_terms( $post_type_ids, 'category',array('fields' => 'ids') );
        if($post_type_cats){
          $post_type_cats = array_unique($post_type_cats);
          $post_type_cats = implode(',',$post_type_cats);
          wp_list_categories('include='.$post_type_cats);
        }
      }
    ?>

    Change “my_custom_post_type” to the post type you registered.

    Thread Starter Harryson

    (@harryson)

    Thank you for replying, however that code doesn’t seem to work 🙁

    I tried debugging it with print_r():
    $post_type_ids = Array ( [0] => 176 )
    $post_type_cats = Array ( )

    I’m not familiar with wordpress code at all, so I have no idea where to go from here.

    Thread Starter Harryson

    (@harryson)

    Scratch that, my bad. Didn’t have any posts hooked to those categories. Problem solved, thanks 🙂

    However, how do I get tags and the quantity they are being used to generate the tag cloud?

    Also, I’m using these categories and tags on a custom post-type => “News”. So my post-type slug is news: How can I make the categories and tags lists to link to example.com/news/category/CATNAME and example.com/news/tags/TAGNAME

    Thread Starter Harryson

    (@harryson)

    Checking out http://codex.wordpress.org/Template_Tags/wp_list_categories I cannot figure out how to make it return raw array and not formatted lists or anchors with <br /> at the end.

    Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter Harryson

    (@harryson)

    Not sure, what would be the perks of using custom taxonomies? Currently for the “news system” I only needs categories and tags.

    Moderator keesiemeijer

    (@keesiemeijer)

    Not sure, what would be the perks of using custom taxonomies?

    Then you can use the native WordPress functions to show a list of categories and tags associated with “news” posts.
    http://codex.wordpress.org/Template_Tags/wp_list_categories
    http://codex.wordpress.org/Function_Reference/get_categories
    http://codex.wordpress.org/Function_Reference/#Category.2C_Tag_and_Taxonomy_Functions

    Custom taxonomies can be like categories or like tags, so if there’s no reason they have to be the same as “Post” categories and tags you should go with custom taxonomies.

    Also, your urls will be: site.com/taxonomy-name/term-name/

    Thread Starter Harryson

    (@harryson)

    Ahaa. I see. That was my next question, how will I get the category links correct. So meaning, that if I use custom post-type such with the slug “news”, and then make custom taxonomy for that “news system”, then I can use the categories and tags links like “example.com/news/categories/films” easier, then with the standard post taxonomies?!

    Thread Starter Harryson

    (@harryson)

    However, doesn’t that mean, that my custom taxonomy will be shown next to the normal category one? I mean, that if I’m using custom post-type, then I would wish that there is only my category (custom) and not the standard one, right next to it.

    And also, wouldn’t the slugs of post-type “news” and custom taxonomy “news” get into a conflict?

    Moderator keesiemeijer

    (@keesiemeijer)

    And also, wouldn’t the slugs of post-type “news” and custom taxonomy “news” get into a conflict?

    Yes they would be in conflict. Even a Page “news” would be in conflict. You can override the default URL settings with 'rewrite' => array( 'slug' => 'news-stories' ),
    http://codex.wordpress.org/Function_Reference/register_taxonomy

    Thread Starter Harryson

    (@harryson)

    Ok ok, I see. I have one last question.

    How do I make it so, if I have custom post type like news. How do I view the category now? So the normal url is example.com/news/ and the categories should be example.com/news/category/films. I thought it should be as easy as THEME_DIR/category-POSTTYPENAME.php. But it doesn’t work..

    I’m also facing the same problem with pagination example.com/page/PAGENR etc.. Can’t I simply use .htaccess somehow?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to get the lists of categories used in custom post type?’ is closed to new replies.