最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to filter custom post types by custom category taxonomy

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Oct 14, 2016 at 17:31 klewis asked Oct 14, 2016 at 16:47 klewisklewis 8991 gold badge14 silver badges29 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6

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'
        )
    )
)

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);
发布评论

评论列表(0)

  1. 暂无评论