douyousu9691 2017-05-18 15:29
浏览 53

如何从Wordpress查询中的自定义帖子类型按ID获取类别

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?

  • 写回答

1条回答 默认 最新

  • doufud21086 2017-05-18 15:44
    关注

    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'   => '='
               ),
            )        
        )
    );
    
    评论

报告相同问题?