【发布时间】:2020-02-05 21:01:58
【问题描述】:
我正在使用插件代码片段和高级自定义字段。我正在使用带有 WP_QUERY 的代码 sn-p 从数据库中获取帖子。我正在尝试从转发器字段中获取值。这不是我的项目,所以我无法真正更改中继器字段。所以我必须处理。这是查询。
$args = array(
'numberposts' => $limit,
'post_type' => 'opleidingen',
'orderby' => 'meta_value',
'suppress_filters' => 'false',
'meta_query' => array(
array(
'key' => 'data_$_startdatum',
'value' => $today,
'compare' => '>',
),
),
'order' => $order,
);
$the_query = new WP_Query( $args );
我想要做的是将今天的日期与 data_$_startdatum 进行比较,其中 startdatum 是一个子字段。我在这里读到https://www.advancedcustomfields.com/resources/query-posts-custom-fields/,这是对子字段进行 where 比较的方法。但如果我运行查询,我会得到 0 个帖子。
我已经通过将 $ 替换为 0 来测试查询是否有效并且有效。但仅适用于转发器字段的第一个索引。如果我使用 OR 运算符并将带有 data_0_startdatum 的数组添加到 data_10_startdatum 它适用于前 10 个索引。就这样
$args = array(
'numberposts' => $limit,
'post_type' => 'opleidingen',
'orderby' => 'meta_value',
'suppress_filters' => 'false',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'data_0_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_1_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_2_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_3_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_4_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_5_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_6_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_7_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_8_startdatum',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'data_9_startdatum',
'value' => $today,
'compare' => '>',
)
),
'order' => $order,
);
但我希望它适用于整个数组长度。从 Wordpress 4.8.3 开始,您不能使用 % 并且必须使用 post_where 过滤器将 % 替换为 LIKE。我尝试通过在我的functions.php中实现以下代码来做到这一点
function my_posts_where( $where ) {
echo($where);
$where = str_replace("meta_key = 'data_$", "meta_key LIKE 'data_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
这不起作用。所以我尝试用代码 sn-ps 插件制作一个 sn-p 并赋予它高优先级,但这也不起作用。我也试图给它一个低优先级,这也没有奏效。我在过滤器中放了一个回声,在浏览器中查看帖子时返回以下内容
AND (wp_posts.ID = '610') AND wp_posts.post_type = 'page' AND wp_posts.post_type = 'wpmm_theme' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_author = 6 AND wp_posts.post_status = 'private') AND (wp_posts.ID = '610') AND wp_posts.post_type = 'page' AND (wp_posts.ID = '610') AND wp_posts.post_type = 'page'
我的查询不在其中?当我将过滤器放在我的functions.php或具有高或低优先级的单独sn-p中时,我得到了这个结果。
【问题讨论】:
标签: php wordpress advanced-custom-fields