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