admin管理员组文章数量:1125598
Update I think I've completed this, but please clean up if there is a better way.
Code is the new working version
Original Issue This page template query works & displays the custom posts items based on 2 criteria. A category & part match of the address field.
My issue now is this page template is only good for 1 exact match.
I want to create a new custom post type with 2 fields and want the query to pull the data from there. So I can then use this template across all those custom posts and change the Category & Address from the front end to spin up a new page.
Variables to change in the code below:
terms' = "Electricians"
'value' = "London"
I have added a meta box to the pages where i will be storing the variable values.
//CODE FOR ADDING META FIELDS To PAGES - This is in functions.php//
function get_custom_service($field_name){
return get_post_meta(get_the_ID(),$field_name,true);
}
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'location pages', 'online-generator' ),
'id' => 'bizpages_location',
'post_types' => ['page'],
'context' => 'normal',
'fields' => [
[
'type' => 'text',
'name' => esc_html__( 'location', 'online-generator' ),
'id' => $prefix . 'location_biz',
],
[
'type' => 'text',
'name' => esc_html__( 'service', 'online-generator' ),
'id' => $prefix . 'service_biz',
],
],
];
return $meta_boxes;
}
//CODE that grabs all results based on 2 factors. This is a page template added to the page//
$nl = new WP_Query( array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'listing-categories',
'field' => 'slug',
'terms' => get_custom_service('service_biz'),
'operator' => 'AND'
),
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'address',
'value' => get_custom_service('location_biz'),
'compare' => 'LIKE'
)
) ));
本文标签: wp queryWPQueryVariable input
版权声明:本文标题:wp query - WP_Query - Variable input 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736669170a1946822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论