如何在过滤后使用自定义字段值修改Wordpress WP_Query?
我有三个选择框允许过滤自定义帖子列表,可以选择任何或全部选项,然后单击“过滤器”按钮,然后重新加载页面并将过滤的变量添加到该URL作为查询字符串。这适用于这两个自定义分类完全没有问题,我有:如何在过滤后使用自定义字段值修改Wordpress WP_Query?
$categories = array('company-sector', 'company-location');
foreach ($categories as $category) {
if (isset($_GET[$category]) && !empty($_GET[$category])) {
$query['tax_query'][] = array(
'taxonomy' => $category,
'field' => 'slug',
'terms' => $_GET[$category]
);
}
}
然而,在使用第三过滤器(与ACF创建一个自定义字段)相同的逻辑,帖子列表不会改变:
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
$query['meta_query'][] = array(
'key' => 'company_status',
'value' => $_GET['company-status'],
);
}
此元查询工作就好了,如果我手动添加它到初始WP_Query像这样:
$query = array(
'post_type' => 'company-post',
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'company_status',
'value' => 'acquired'
)
)
);
但是,试图将其添加到初始查询得到可变FR后om查询字符串似乎不工作,我在这里做错了什么?
//编辑
即使这是工作:
$query = array(
'post_type' => 'company-post',
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'company_status',
'value' => $_GET['company-status']
)
)
);
所以它必须是事做我如何追加meta_query初始WP_Query ...
我想你可以使用pre_get_posts这个在你的functions.php
// Load our function when hook is set
add_action('pre_get_posts', 'js-modifying-main-query');
function js-modifying-main-query($query) {
$status = $_GET['company-status'];
if($status && $status != ""){
if(!is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'company-post') {
$query->set('meta_key', 'company_status');
$query->set('meta_value', $status);
}
}
}
没有测试编辑它 - 只是一个初步的想法。
//编辑
要查看您的查询的变量,你可以使用这个:
global $wp_query;
var_dump($wp_query->query_vars);
我仍然不知道为什么我最初的代码是不工作的,但我已经成功地拿出这似乎做什么,我就需要一个解决方法:
$status = false;
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
$status = array(
'key' => 'company_status',
'value' => $_GET['company-status'],
);
}
$query = array(
'post_type' => 'company-post',
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array($status)
);
//编辑
即使这是工作:
$status = false;
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
$status[] = array(
'key' => 'company_status',
'value' => $_GET['company-status'],
);
}
$query = array(
'post_type' => 'company-post',
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => $status
);
但只要我试试这个,它失败:
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
$query['meta_query'][] = array(
'key' => 'company_status',
'value' => $_GET['company-status'],
);
}
$query = array(
'post_type' => 'company-post',
'posts_per_page' => $posts_per_page,
'orderby' => 'title',
'order' => 'ASC',
);
您推动company-status
错误的值。
if (isset($_GET['company-status']) && !empty($_GET['company-status'])) {
$query['meta_query'][] = array(
array(
'key' => 'company_status',
'value' => $_GET['company-status'],
)
);
}
...让这个:
Array
(
[0] => Array
(
[key] => company_sector
[value] => val1
)
[1] => Array
(
[key] => company_status
[value] => val2
)
[2] => Array
(
[0] => Array
(
[key] => company_status
[value] => val3
)
)
)
你应该推到meta_query
数组是这样的:
$query['meta_query'][] = array(
'key' => 'company_status',
'value' => $_GET['company-status'],
);
另外,如果参数relation
是中省略,查询将检查所有键和值 - 但我假设你知道这一点。
我已经试过两种方法,我忘了在我的第一个问题中提到这一点。我已更新我的问题以反映这一点。我还根据您的建议更新了我的答案,并进行了一些进一步的测试,但似乎仍然无法正常工作。 此外,如果需要'关系',那么这不会改变我如何推到meta_query? – JFK1980
请发布您的'$ query'变量的'print_r',我认为这就是问题所在。 –
如果您的var_dump($ _ GET ['company-status'])是否获得了预期的结果? – Stender
还没有尝试var_dump(),但是echo $ _GET ['company-status'];返回我期待看到的。 – JFK1980
var_dump()返回:string(7)“current”。 ('当前'或'已获得'是目前的两种选择,在后台显示为ACF单选按钮。) – JFK1980