How to filter custom post types by custom category taxonomy

Question

I’m running WordPress 4.6.1 and I am trying to learn how to filter custom post types by a category taxonomy process. This is very helpful as non-technical folks can easily filter custom post type posts by category in the admin.

This is my setup…

  1. I’m building a child theme off of tweentysixteen

  2. I created and registered a custom post type in my child functions.php file like this…

    add_action('init','prowp_register_my_post_types');
    function prowp_register_my_post_types() {
        register_post_type('products',
        array(
            'labels' => array (
                'name' => 'Products',
                'singular_name' => 'Product',
                'add_new' => 'Add New Product',
                'add_new_item' => 'Add New Product',
                'edit_item' => 'Edit this Product',
                'new_item' => 'New Product',
                'all_items' => 'All My Products'
            ),
            'public' => true,
            'show_ui' => true,
            'taxonomies'  => array ( 
                'category' 
            ),
            'supports' =>  array (
                'title', 
                'revisions', 
                'editor', 
                'thumbnail', 
                'page-attributes', 
                'custom-fields')
        ));
    }
    
  3. I am now using my registered custom post type in my child index.php file like this:

    $pargs = array(
            'post_per_page' => '-1',
            'post_type' => 'products',
            'tax_query' => array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms' => 'Specials'
            )
        );
        $myProducts = new WP_Query($pargs);
        while ( $myProducts->have_posts() ) : $myProducts->the_post();
            get_template_part('template-parts/products',get_post_format());
        endwhile;
        rewind_posts();
        wp_reset_postdata();
    
  4. Finally, from wp-admin, I then created my custom post types posts and assigned the category “Specials” to “one” of my posts. The others are uncategorized. And every page is published.

…But for some reason, my browser page is listing all my posts from this custom post type, and not just Specials. I’m I doing something wrong?

0
klewis 1 month 0 Answers 8 views 0

Leave an answer