1

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?

3

You are doing a little mistake in your $pargs

As per documentation

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). Also you have "post_per_page" instead of "posts_per_page"

$pargs = array(
    'posts_per_page' => '-1',
    'post_type' => 'products',
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms' => 'specials'
        )
    )
  • haha - its always something so freakn simple that is overlooked! Thank you so much!! – klewis Oct 14 '16 at 17:54
  • @blackhawk ... yeah happens with everybody !! :) – Anish Oct 14 '16 at 17:58
0

You can try with the following codes:

$terms = wp_get_post_terms( $post->ID, array('category') );
$term_slugs = wp_list_pluck( $terms, 'slug' );    
$args = array(
        'post_per_page' => '-1',
        'post_type' => array( 'products' ), 
            'tax_query' => array(
                array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $term_slugs
                )  
    );


    $my_query = null;
    $my_query = new WP_Query($args);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.