admin管理员组文章数量:1122846
Currently, I have a query that searches for all employees with a taxonomy term that matches the word that is searched for in an input field. That works perfectly fine but my client now wants the employee results to be displayed in alphabetical order by last name which is a custom field that I created in the admin panel using Toolset Types.
Whenever I try to add
'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'DESC'
to the list of arguments, it breaks the whole query.
Here is the working code below:
$names = array(
'fields' => 'names'
);
$prac_names = get_terms('attorney-practice', $names);
$prac_matches = array_shift(preg_grep ('('. $search .')', $prac_names));
$off_names = get_terms('office-location', $names);
$off_matches = array_shift(preg_grep('('. $search .')', $off_names));
if(in_array($prac_matches, $prac_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'attorney-practice',
'field' => 'name',
'terms' => $prac_matches
)
)
);
}elseif(in_array($off_matches, $off_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'office-location',
'field' => 'name',
'terms' => $off_matches
)
)
);
}
$query = new WP_Query( $args );
Currently, I have a query that searches for all employees with a taxonomy term that matches the word that is searched for in an input field. That works perfectly fine but my client now wants the employee results to be displayed in alphabetical order by last name which is a custom field that I created in the admin panel using Toolset Types.
Whenever I try to add
'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'DESC'
to the list of arguments, it breaks the whole query.
Here is the working code below:
$names = array(
'fields' => 'names'
);
$prac_names = get_terms('attorney-practice', $names);
$prac_matches = array_shift(preg_grep ('('. $search .')', $prac_names));
$off_names = get_terms('office-location', $names);
$off_matches = array_shift(preg_grep('('. $search .')', $off_names));
if(in_array($prac_matches, $prac_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'attorney-practice',
'field' => 'name',
'terms' => $prac_matches
)
)
);
}elseif(in_array($off_matches, $off_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'office-location',
'field' => 'name',
'terms' => $off_matches
)
)
);
}
$query = new WP_Query( $args );
Share
Improve this question
asked Dec 13, 2017 at 20:10
Vanessa CeballosVanessa Ceballos
1
1 Answer
Reset to default 0You can use a meta_query
in addition to your tax_query :
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
I have in which if
statement you want to use it so I put it in the of the code.
$names = array(
'fields' => 'names'
);
$prac_names = get_terms('attorney-practice', $names);
$prac_matches = array_shift(preg_grep ('('. $search .')', $prac_names));
$off_names = get_terms('office-location', $names);
$off_matches = array_shift(preg_grep('('. $search .')', $off_names));
if(in_array($prac_matches, $prac_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'attorney-practice',
'field' => 'name',
'terms' => $prac_matches
)
)
);
}elseif(in_array($off_matches, $off_names)) {
$args = array(
'post_type' => 'employee',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'office-location',
'field' => 'name',
'terms' => $off_matches
)
)
);
}
// Here you add your meta_query to get the value from post_meta
$args['meta_query'] => array(
array(
'key' => 'wpcf-last-name',
'value' => 'YOUR VALUE',
'compare' => 'LIKE',
),
);
// Then you set the order with your
$args['orderby'] => 'meta_value',
$args['order'] => 'DESC'
$query = new WP_Query( $args );
Hope it helps :)
本文标签: Order Wordpress Query by Custom Field While Still Using Taxquery Argument
版权声明:本文标题:Order Wordpress Query by Custom Field While Still Using Tax_query Argument 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308503a1933686.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论