1
votes

I have a custom post type called resources:

register_post_type(
    'resources',
    tp_build_post_args(
        'resources', 'Resource', 'Resources',
        array(
            'menu_icon'     => 'dashicons-welcome-write-blog',
            'menu_position' => 20,
            'public'      => true,
            'supports' => array('editor', 'title','author','thumbnail', 'revisions'),
            'taxonomies' => array('sector', 'subject', 'type'),
        )
    )
);

And a taxonomy called type:

register_taxonomy(  
    'type', 
    'type', 
    array(  
        'hierarchical' => true,  
        'label' => 'Type',  
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'type', 
            'with_front' => false 
        ) 
    )
);

Type can be one of the following:

  • Blog
  • News
  • Webinar

I'm trying to use wp_query to display all posts under resources with the type "News". However, it's returning posts all the posts.

Where is my approach breaking apart:

$card_count = 4;
$resource_type = 'News';

$args = array(  
    'post_type' => 'resources',
    'post_status' => 'publish',
    'posts_per_page' => $card_count,
    //'cat' => $resource_type,
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'terms' => $resource_type,
        )
    )
);
2

2 Answers

0
votes

Try it:

'tax_query' => array( array( 'taxonomy' => 'type', 'terms' => $resource_type, 'field' => 'name', // or 'slug' 'operator' => 'IN' ) ),

0
votes

My guess is that you need to specify that what you are looking for is the name of the taxonomy, it might be looking for ID by default.

$args = array(  
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => $card_count,
//'cat' => $resource_type,
array(
        'taxonomy'         => 'type',
        'terms'            => $resource_type,
        'field'            => 'name',
        'operator'         => 'IN',
        'include_children' => true,
    )
);

you can read the docs to get a better understanding - Or if you want to cheat a bit, this is a decent generator : https://generatewp.com/wp_tax_query/