按分类自定义字段筛选自定义帖子

时间:2015-05-04 21:36:08

标签: wordpress advanced-custom-fields

我正在使用Advanced Custom Field Plugin并且我试图通过分类字段过滤一些自定义帖子,修改WP_Query:

$wp_query = new WP_Query(
    array(
        'post_type' => 'recursos', // My custom post
        'posts_per_page' => 12,
        'meta_key' => 'recursos_tipo', // My Taxonomy custom field name
        'meta_value' => 'documentos' // My taxonomy slug; the value for filter
    )
)

如果我尝试按文本字段进行过滤,一切都很好,WP_Query就会被修改。但是当该字段是Taxonomy字段时,我不知道我应该传递什么参数,因为它是一个对象。我已经尝试了分类标准名称和分类标识但不起作用。

是否可以通过分类字段进行过滤?我应该通过'meta_value'的哪个参数?谢谢!

更新 - 结构:

自定义帖子:' recursos'。
Custom Taxonomy Slug:' recursos-tipos' (Group Taxonomy Slug)。
自定义分类:' documentos' (分类学弹头)。
自定义分类标识ID:16 ACF分类标准字段:' recursos_tipo'。

更新 - ' tax_query'

我也试过这个,但是没有用。给我看看所有帖子:

$wp_query = new WP_Query(
    array(
        'post_type' => 'recursos',
        'posts_per_page' => 12,
        'paged' => $paged,
        'tax_query' => array(
            'taxonomy' => 'recursos-tipos',
            'field' => 'slug',
            'terms' => 'documentos'
        )
    )
);

重要提示:我认为这不起作用,因为我"分配"通过ACF分类法字段进行分类,并且它不会影响税收。我的Taxonomies有0个帖子。如果Tax有帖子,tax_query可以正常工作。 There is a way to affect the post count of Custom Taxonomy via ACF Taxonomy Field?

1 个答案:

答案 0 :(得分:0)

您是否尝试过WordPress自定义查询 args ,只需将“Custom_tax”替换为您的值: 如下所示:WordPress WP_Query

<?php

$jabelquery = new WP_Query(  array( 
'post_type' => 'recursos',  // post,page, revision, custom_post_type
'tax_query' => array(                     //(array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND',                      //(string) - Possible values are 'AND' or 'OR' and is the equivalent of ruuning a JOIN for each taxonomy
  array(
    'taxonomy' => 'recursos-tipos',                //(string) - Taxonomy.
    'field' => 'slug',                    //(string) - Select taxonomy term by ('id' or 'slug')
    'terms' => array( 'recursos_tipo' )                 //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
     )
) ) 
 );
 // The Loop
 if ( $jabelquery->have_posts() ) :
 while ( $jabelquery->have_posts() ) : $jabelquery->the_post(); ?>


 <?php the_title(); ?>


 <?php endwhile; endif; ?>  

然后你可以用这样的ACF字段替换custom_tax:

$jab_tax = get_field('taxonomy_custom_select');
'taxonomy' => $jab_tax,
相关问题