首页 文章

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

提问于
浏览
0

我正在使用Advanced Custom Field Plugin并且我正在尝试通过 Taxonomy Field 过滤一些自定义帖子,修改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
    )
)

如果我尝试过滤 Text Field 一切都很好,WP_Query就会被修改 . 但是当该字段是分类法字段时,我不会尝试分类标准名称和分类标识但不起作用 .

是否可以通过 Taxonomy Field 过滤?我应该通过 'meta_value' 的哪个参数?谢谢!

UPDATE - Structure:

自定义帖子:'recursos' .
Custom Taxonomy Slug:'recursos-tipos'(Group Taxonomy Slug) .
自定义分类:'documentos'(Taxonomy Slug) .
自定义分类ID:16 .
ACF分类法字段:'recursos_tipo' .

UPDATE - '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'
        )
    )
);

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

1 回答

  • 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,
    

相关问题