我有一本出版物Post Type, a编剧Taxonomy 和出版物自定义“元框”值;parent_id
.
在我的archive-publictions-post.php
模板我正在尝试筛选WP_Query
类,具有tax_query
和meta_query
参数。
使用下面的代码,我可以在页面加载时显示所有出版物,当我同时选择一个术语和一个父项时,我可以获得过滤结果,但当我单独选择时,我不能得到过滤结果?
我不确定meta_query
&;tax_query
relationship
在这种情况下应使用参数?
<?php
// The code from: archive-publictions-post.php
// Set Var Filter
$publications_filter = $_POST[\'publications\'];
// Set Terms Arguments
$term_args = array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => false,
\'parent\' => 0,
);
// Get Publications Terms
$terms = get_terms(\'publications-tax\', $term_args);
$writers_args = array(
\'post_type\' => \'writers-post\',
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'order\' => \'DESC\',
);
// Get Writers Posts
$writers = get_posts($writers_args);
echo \'<select name="publications[]" multiple>\';
// Set var as an array
$all_writers = array();
foreach($writers as $writer){
// Set all terms to our array
$all_writers[] = $writer->ID;
echo \'<option value="\'.$writer->ID.\'">\'.$writer->post_name.\'</option>\';
}
foreach($terms as $term){
// Set all terms to our array
$all_terms[] = $term->name;
echo \'<option value="\'.$term->name.\'">\'.$term->name.\'</option>\';
}
echo \'</select>\';
echo \'<input type="submit" value="go">\';
?>
</form>
WP_QUERY $args
:
//Start Args
$publications_results = isset($_POST[\'publications\']) ? $publications_filter : $all_terms;
$publication_values=array();
$writers=array();
foreach ($publications_results as $key => $value) {
if (is_numeric($value)) {
$writers_values[] = $value;
}
else{
$publications_values[] = $value;
}
}
$args = array(
\'post_type\' => array(\'publications-post\',\'writers-post\'),
\'order\' => \'DESC\',
\'post_status\' => \'any\',
\'orderby\' => \'date\',
\'tax_query\' => array(array(
\'taxonomy\' => \'publications-tax\',
\'field\' => \'slug\',
\'terms\' => $publications_values
))
);
if($_POST[\'publications\']){
$args[\'meta_query\'][] =
array(
\'key\' => \'parent_id\',
\'value\' => $writers_values,
\'type\' => \'numeric\',
);
}
谢谢
SO网友:user1575949
我的初始代码没有提供默认值WP_query
参数参数如果未设置任何一个Select选项-空参数将过滤掉所有内容。
尽我所能,我应用了初始Post和Terms查询中的默认参数值,现在select过滤器正在工作。
我很高兴知道我的代码是否可以改进:)
/*
*Get Publications Filters
*/
$writers_filter = array();
$publications_filter = array();
if (isset($_POST[\'publications\'])) {
$publications_filter = $_POST[\'publications\'];
// Separate Numeric from String - i.e. Post ID and Term Name
foreach ($publications_filter as $key => $value) {
if (is_numeric($value)) {
$writers_filter[] = $value;
}
else{
$terms_filter[] = $value;
}
}
}
// If Either Select Option is Empty,
// Assign Final Value With All Queried Results - Avoid Empty Value
$final_writers = array();
$final_terms = array();
$final_writers = (!empty($writers_filter)) ? $writers_filter : $all_writers;
$final_terms = (!empty($terms_filter)) ? $terms_filter : $all_terms;
/*
*Set WP_Query Argument Parameters
*/
$args = array(
\'post_type\' => array(\'publications-post\',\'writers-post\'),
\'order\' => \'DESC\',
\'post_status\' => \'any\',
\'orderby\' => \'date\',
\'tax_query\' => array(array(
\'taxonomy\' => \'publications-tax\',
\'field\' => \'slug\',
\'terms\' => $final_terms
))
);
if($_POST[\'publications\']){
$args[\'meta_query\'][] =
array(
\'key\' => \'parent_id\',
\'value\' => $final_writers,
\'type\' => \'numeric\',
);
}