【问题标题】:Filter Custom Posts by Taxonomy Custom Field按分类自定义字段过滤自定义帖子
【发布时间】:2015-07-14 10:41:44
【问题描述】:

我正在使用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 会被修改。但是当该字段是分类字段时,我不知道应该传递什么参数,因为它是一个对象。我已经尝试了分类名称和分类 ID,但不起作用。

是否可以通过分类字段进行过滤?我应该为'meta_value' 传递什么参数?谢谢!

更新 - 结构:

自定义帖子:'recursos'。
自定义分类蛞蝓:'recursos-tipos'(组分类蛞蝓)。
自定义分类法:'documentos'(Taxonomy Slug)。
自定义分类 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 分类字段“分配”分类,并且它不会影响税收。我的分类有 0 个帖子。如果税务部门有帖子,tax_query 可以正常工作。 There is a way to affect the post count of Custom Taxonomy via ACF Taxonomy Field?

【问题讨论】:

  • 能否提供您用于查询分类字段的代码?
  • @RobinVinzenz 我用我尝试过的代码更新了我的问题。我不知道这是不是你要求的。
  • 尝试在 meta_value 后面添加分类 slug 的 id,如下所示:'documentos_'。如果那行得通,我会发布答案,我还不确定
  • 也许会对你有所帮助:wordpress.stackexchange.com/questions/73803/… 用于 meta_value:“需要的第二个参数是格式为“$type_$id”的字符串。
  • @RobinVinzenz 我尝试过使用 'taxonomy_id' 字符串格式但不起作用:(

标签: wordpress advanced-custom-fields


【解决方案1】:

您是否尝试过 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,

【讨论】:

  • 我正在尝试应用你的答案,但我有点困惑......我用结构更新了我的问题,你能给我一个结构的例子吗?对不起!我很困惑。
  • 我用“tax_query”做了一个测试,但没有用。用代码更新我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多