Grow your CSS skills. Land your dream job.

WordPress: show custom post types, by category

  • # May 17, 2013 at 5:01 pm

    So I have a custom post type called “shoes”. I know I can display these if I create a template called archive-shoes.php (for the listing) and single-shoes.php (for each individual post), and the listing can be shown by going to http://localhost/shoes, which will then list ALL posts of type “shoes”.

    So far so good.

    Now, each shoe is either “outdoor” or “indoor”. I could do this through adding categories, or custom fields. Either way…..

    Question is, if I want to show ONLY the outdoor shoes, what URL should I use? Am I going to have to use http://localhost/shoes/?type=outdoor (and then do the filtering in archive-shoes.php) or is this functionality already built in in WordPress and there a way I can directly call it?

    I thought about creating category-outdoor.php but it seems it will then not list the custom post types, only REGULAR posts with category “outdoor”.

    I may be overthinking it but what’s the most efficient way of listing all posts of a custom type with a certain category?

    Thanks,

    # May 17, 2013 at 6:07 pm

    What about creating custom taxonomies for your “shoes” post type? Then you could do http://localhost/outdoor and http://localhost/indoor?

    I admit, I have only a *basic* working knowledge of the full power of custom taxonomies because I haven’t used them that much, but this sounds like a perfect application for them.

    # May 18, 2013 at 8:55 am

    That would work if the custom taxonomy would be a dropdown field in the admin, but from the looks of it, it’s not…just a text field in which the admin can enter any value they like.

    I also don’t have much experience with taxonomies (I still get confused what the difference is between a custom taxonomy and a custom field) but I’d prefer it to be a custom field. I think.

    # May 18, 2013 at 12:00 pm

    wptuts has a tutorial to change the taxonomies to radio button selections: http://wp.tutsplus.com/tutorials/creative-coding/how-to-use-radio-buttons-with-taxonomies/

    I still think taxonomies would be your best approach to this. I always visualize taxonomies as “custom categories” for custom post types.. when registering the taxonomy, you specify that it only shows up in the “shoes” post type, and it becomes a category filter for that post type.

    # May 18, 2013 at 8:18 pm

    @Senff are you using Custom Post Types UI or is this just manual? What I’ve always done inside of the plugin was change the taxonomy type to “hierarchical” and they seem to function the way you are describing.

    # May 19, 2013 at 12:04 am

    @ChrisP: OK, so I’ve tried the taxonomies route (even though I would have preferred to keep it a custom field, but let’s see if I can get there). I see your point.

    @JoshWhite: yes, through Custom Post Types UI, and with the “hierarchical” setting it gets the select boxes.

    Now…….. I gave my custom post a custom taxonomy setting of “indoor”, but going to http://localhost/indoor doesn’t give me the list of all indoor shoes. :(

    # May 19, 2013 at 12:15 am

    > Now…….. I gave my custom post a custom taxonomy setting of “indoor”, but going to http://localhost/indoor doesn’t give me the list of all indoor shoes. :(

    Have you flushed the permalinks?

    side note: I’m sure it’s possible to use custom fields for this, but keeping a clean URL structure, and keeping the backend work _relatively_ low, I think the taxonomies route would be better IMHO

    # May 19, 2013 at 2:35 am

    Check out the Answer on http://wordpress.stackexchange.com/questions/8100/custom-taxonomy-list-page

    I am not sure if it is exactly what you are trying to do, but you can create page templates to call those custom taxonomies that are in your custom post type. I was trying to do something similar and someone (who is better at WP modifications than me) told me it wasn’t possible and I needed to use page templates instead and modified the queries accordingly. The article I linked above was something I found that kinda explains the situation.

    # May 19, 2013 at 9:12 am

    @scottnix: although that article didn’t give me the exact answer (I’m not creating custom posts and taxonomies with code, but using a plugin), it did point me in the right direction.

    Turns out I won’t see the indoor shoes by going to http://localhost/indoor, but by going to http://localhost/type/indoor

    So the functionality finally works.

    Not 100% satisfied with having to do taxonomies (I would prefer that the client could change all their settings as custom fields, now they have to use custom fields AND taxonomies) but I guess this is the next best thing.

    Thanks everyone!

    # May 21, 2013 at 3:53 pm

    Hey @Senff,

    I’ve quickly created this for you. It’s a plugin that creates Custom Post Type of “Shoes” and has two taxonomies attached: “type” and “color”… Type can be “Indoor or Outdoor” or you may choose to change this… you can create more by adding `$taxonomies = array();` to `$taxonomies = array();`. Just copy the contents on this file and save it in your plugins folder. Hope that helps, Alen.

    CODE REMOVED see bellow >> edited few mistakes…

    few gotchas: you need to flush the permalinks (going to Settings >> Permalink and clicking on Permalink twice) or you [could add automatic flushing](http://codex.wordpress.org/Function_Reference/flush_rewrite_rules).

    I’ve tested the code with WordPress 3.6-beta3-24306. Newest as of today.

    When using menu editor to add items, make sure to click on “screen options” top right corner to display all post types, so you can select them for your menu.

    # May 21, 2013 at 7:34 pm

    < ?php
    /*
    Plugin Name: Custom Post Type & Taxonomies
    Plugin URI: http://#
    Description: Custom Post Type & Taxonomies Description
    Version: 1.0
    Author: Alen Abdula
    Author URI: http://www.alenabdula.com
    */
    class My_Custom_Post_Type_Taxonomy{
    public function __construct()
    {
    $this->register_post_type();
    $this->taxonomies();
    }
    public function register_post_type()
    {
    $args = array(
    ‘labels’ => array(
    ‘name’ => ‘Shoes’,
    ‘singular_name’ => ‘Shoe’,
    ‘menu_name’ => ‘Shoes’,
    ‘all_items’ => ‘Shoes’,
    ‘add_new’ => ‘Add New Shoe’,
    ‘add_new_item’ => ‘Add New Shoe’,
    ‘edit_item’ => ‘Edit Shoe’,
    ‘new_item’ => ‘New Shoe’,
    ‘view_item’ => ‘View Shoe’,
    ‘search_items’ => ‘Search Shoes’,
    ‘not_found’ => ‘No Shoes Found’,
    ‘not_found_in_trash’ => ‘No Shoes in Trash’,
    ‘parent_item_colon’ => ‘Shoes’
    ),
    ‘description’ => ‘My Cool Custom Post Type for Shoes’,
    ‘public’ => true,
    ‘exclude_from_search’ => false,
    ‘publicly_queryable’ => true,
    ‘show_ui’ => true,
    ‘show_in_nav_menus’ => true,
    ‘show_in_menu’ => true,
    ‘menu_position’ => 5,
    ‘map_meta_cap’ => true,
    ‘hierarchical’ => true,
    ‘supports’ => array(
    ‘title’,’editor’,’author’,’thumbnail’,’excerpt’,’trackbacks’,
    ‘custom-fields’,’comments’,’revisions’,’page-attributes’,’post-formats’
    ),
    ‘has_archive’ => true,
    ‘rewrite’ => array(
    ‘slug’ => ‘shoes’,’with_front’ => true,
    ‘feeds’ => true,’pages’ => true
    ),
    ‘query_var’ => ‘shoe’
    );
    register_post_type(‘shoes’, $args);
    }
    public function taxonomies()
    {
    $taxonomies = array();
    //


    COLOR


    $taxonomies = array(
    ‘labels’ => array(
    ‘name’ => ‘Colors’,’singular_name’ => ‘Color’,’menu_name’ => ‘Colors’,’all_items’ => ‘All Colors’,
    ‘edit_item’ => ‘Edit Color’,’view_item’ => ‘View Color’,’update_item’ => ‘Update Color’,
    ‘add_new_item’ => ‘Add New Color’,’new_item_name’ => ‘New Color Name’,’parent_item’ => ‘Parent Category’,
    ‘parent_item_colon’ => ‘Parent Category:’,’search_items’ => ‘Search Colors’,’popular_items’ => ‘Popular Colors’,
    ‘separate_items_with_commas’ => ‘Separate Colors with commas’,’add_or_remove_items’ => ‘Add or remove Colors’,
    ‘choose_from_most_used’ => ‘Choose from the most used Colors’,’not_found’ => ‘No Colors Found’
    ),
    ‘public’ => true,
    ‘show_ui’ => true,
    ‘show_in_nav_menus’ => true,
    ‘show_tagcloud’ => true,
    ‘show_admin_column’ => true,
    ‘hierarchical’ => true,
    ‘query_var’ => ‘color’,
    ‘rewrite’ => array(
    ‘with_front’ => false,
    ‘hierarchical’ => true
    )
    );
    //


    COLOR


    //


    TYPE


    $taxonomies = array(
    ‘labels’ => array(
    ‘name’ => ‘Types’,’singular_name’ => ‘Type’,’menu_name’ => ‘Types’,’all_items’ => ‘All Types’,
    ‘edit_item’ => ‘Edit Type’,’view_item’ => ‘View Type’,’update_item’ => ‘Update Type’,
    ‘add_new_item’ => ‘Add New Type’,’new_item_name’ => ‘New Type Name’,’parent_item’ => ‘Parent Category’,
    ‘parent_item_colon’ => ‘Parent Category:’,’search_items’ => ‘Search Types’,’popular_items’ => ‘Popular Types’,
    ‘separate_items_with_commas’ => ‘Separate Types with commas’,’add_or_remove_items’ => ‘Add or remove Types’,
    ‘choose_from_most_used’ => ‘Choose from the most used Types’,’not_found’ => ‘No Types Found’
    ),
    ‘public’ => true,
    ‘show_ui’ => true,
    ‘show_in_nav_menus’ => true,
    ‘show_tagcloud’ => true,
    ‘show_admin_column’ => true,
    ‘hierarchical’ => true,
    ‘query_var’ => ‘type’,
    ‘rewrite’ => array(
    ‘with_front’ => false,
    ‘hierarchical’ => true
    )
    );
    //


    TYPE


    $this->register_all_taxonomies($taxonomies);
    }
    public function register_all_taxonomies($taxonomies)
    {
    foreach($taxonomies as $name => $arr)
    {
    register_taxonomy($name,array(‘shoes’), $arr);
    }
    }
    }
    add_action(‘init’, function(){
    new My_Custom_Post_Type_Taxonomy();
    });

    # May 22, 2013 at 6:07 pm

    Thanks @AlenAbdula

    I actually have the need for a lot more custom fields and taxonomies (the problem in this thread was a reduced test case) and I’m using a plugin to create custom post types and custom taxonomies, so I can’t directly use your code. However it will definitely help me with creating custom code myself, by seeing how you did this, so thanks :)

    # July 30, 2013 at 9:43 pm

    @senff just simply create a page template, Then you will apply it to a page. Put the following Code into the page template that you created, This code will allow you to query all of the post from any designated category.

    query_posts( array ( ‘category_name’ => ‘my-category-slug’, ‘posts_per_page’ => -1 ) );

    This is the only possible way that i know how to do this. I do know for a fact that it is not possible to accomplish this strictly through using a plugin simply because the plugin developers must have overlooked this functionality and did not add this to the plugins.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".