Forums

[resolved] How to get the lists of categories used in custom post type? (12 posts)

  1. Harryson
    Member
    Posted 6 months ago #

    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

  2. keesiemeijer
    moderator
    Posted 6 months ago #

    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.

  3. Harryson
    Member
    Posted 6 months ago #

    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.

  4. Harryson
    Member
    Posted 6 months ago #

    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

  5. Harryson
    Member
    Posted 6 months ago #

    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.

  6. keesiemeijer
    moderator
    Posted 5 months ago #

  7. Harryson
    Member
    Posted 5 months ago #

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

  8. keesiemeijer
    moderator
    Posted 5 months ago #

    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/

  9. Harryson
    Member
    Posted 5 months ago #

    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?!

  10. Harryson
    Member
    Posted 5 months ago #

    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?

  11. keesiemeijer
    moderator
    Posted 5 months ago #

    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

  12. Harryson
    Member
    Posted 5 months ago #

    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?

Reply

You must log in to post.

About this Topic