【问题标题】:Advanced Custom Fields: querying posts filtered by custom field values高级自定义字段:查询按自定义字段值过滤的帖子
【发布时间】:2015-01-11 22:15:29
【问题描述】:

我正在尝试根据子自定义字段值查询我的 CPT 帖子,如示例 5 this tutorial.

我的 CPT(名为“trip”)有一个名为“departure_date”的重复字段,其中有一个名为“departure_day”的子字段:

function my_posts_where( $where ) {
    $where = str_replace("meta_key = 'departure_date'", "meta_key LIKE 'departure_date'", $where);
    return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
    'numberposts' => -1,
    'post_type' => 'trip',
    'meta_query' => array(
        array(
            'key' => 'departure_day',
            'value' => 0,
            'compare' => '>'
        )
    )
);
$get_trips_date = new WP_Query( $args );
if( $get_trips_date->have_posts() ):
    while ( $get_trips_date->have_posts() ) : $get_trips_date->the_post();
        if( get_field('departure_date') ) {
            while( has_sub_field('departure_day') ) { 
                echo get_sub_field('departure_day');
            }
        }
    endwhile;
endif; 
wp_reset_query();

尽管为所有帖子填充了子字段“departure_day”,但此代码不返回任何内容。为什么?

【问题讨论】:

  • 我可能错了,但我不认为这个过滤器是为 wp_query 调用的。它适用于 get_posts。但是尝试在 $args 中传递 'suppress_filters'=>false。你可能会走运!
  • 谢谢,但没用。代码也来自插件开发人员,他们正在使用 wp_query() 所以我也在做同样的事情。
  • var_dump $where .... 你需要确保你有正确的短语来改变。似乎 wp_query 确实运行了过滤器。查看查询中的键名,您将其更改为类似离开日期。你最后还说,帖子正在被退回?
  • 谢谢,var_dump($where) 显示如下: string(218) " AND wp_15_posts.post_type = 'trip' AND (wp_15_posts.post_status = 'publish' OR wp_15_posts.post_status = 'private') AND ( (wp_15_postmeta.meta_key = 'departure_day' AND CAST(wp_15_postmeta.meta_value AS CHAR) > '0') )"。不知道该怎么做。我正在使用本地多站点 WP,我的项目只是此多站点设置中的一个 WP 站点。哦,很抱歉造成混乱,在我的问题结束时,我只是说在仪表板中我为每个帖子填充了子字段。
  • 好吧,如果您有帖子元设置为“出发日”的帖子,如果元键大于 0,这将返回帖子。但我猜您是否已将日期/日期保存为文本?在这种情况下它总是等于 0 你需要 var_dump 返回值和 qet_post_meta() 来弄清楚这里发生了什么。

标签: wordpress custom-post-type advanced-custom-fields


【解决方案1】:

你的 $args 应该是这样的

`

$args = array(
    'numberposts' => -1,
    'post_type' => 'trip',
    'meta_query' => array(
        array(
            'key' => 'departure_date_%_departure_day',
            'value' => 0,
            'compare' => '>'
        )
    )
);

`

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2018-10-13
    • 2016-06-25
    • 2021-06-29
    • 1970-01-01
    • 2015-07-14
    • 2015-10-24
    • 1970-01-01
    相关资源
    最近更新 更多