简体   繁体   English

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

[英]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" . 我正在尝试构建WP_Query ,我只想获取分类法(名称为“ give_forms_category”的自定义帖子类型类别)为“ 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? 有人可以给我提示吗?

with custom fields you should use a special approach, explained in WP Codex 对于自定义字段,您应该使用一种特殊的方法,如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'   => '='
           ),
        )        
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM