Luke
Luke

Reputation: 127

How to get category by ID from a custom post type in a Wordpress query

I am trying to build a WP_Query and I want to fetch only the posts where the taxonomy (custom post type category with the name "give_forms_category") is "18". I have a working query for the regular post type but I'm trying to adapt it for my custom post type:

$the_query = new WP_Query( array ( 
    'posts_per_page' => $atts['posts'], 
    'post_type' => array( 'give_forms' ), array( 'cat' => 18 ) )

Can someone give me a hint?

Upvotes: 0

Views: 359

Answers (1)

Stanislav Terletskyi
Stanislav Terletskyi

Reputation: 2112

with custom fields you should use a special approach, explained in WP Codex

You might have something like this:

$the_query = new WP_Query( array ( 
       'posts_per_page' => $atts['posts'],
       'meta_key'  => 'give_forms_category',     
       'post_type'     => 'post',
       'meta_query'    => array (
           'relation'      => 'AND',
           array (
               'key'       => 'give_forms_category',
               'value'     => 18,
               'compare'   => '='
           ),
        )        
    )
);

Upvotes: 1

Related Questions