WordPress.org

Forums

Meta Query - search key values only if the search variable exists? (1 post)

  1. ikjosh
    Member
    Posted 2 years ago #

    Hi,

    I am running a search query and on my search page I have various fields for advanced searching for custom fields. However, if one of the custom fields is not filled out, then it displays no results. In other words, all the fields have to have a proper value for it to show any results. I don't want that. I want it to show the relevant results if they just entered a name and city, or if it was just a last name and state, etc.

    Here is the code, any help is greatly appreciated.

    $searchArgs = array(
    				'post_type' => 'firm',
    				'meta_query' => array(
    					'relation' => 'AND',
    					array( 'key' => 'wpcf-city', 'compare' => 'LIKE',   'value' => $_REQUEST['city'] ),
    					array( 'key' => 'wpcf-state', 'compare' => 'LIKE', 'value' => $_REQUEST['state'] ),
    					array( 'key' => 'wpcf-country', 'compare' => 'LIKE', 'value' => $_REQUEST['country'] ),
    					array( 'key' => 'wpcf-first', 'compare' => 'LIKE', 'value' => $_REQUEST['fname'] ),
    					array( 'key' => 'wpcf-last', 'compare' => 'LIKE', 'value' => $_REQUEST['lname'] ),
    					array( 'key' => 'wpcf-practice-area-id', 'compare' => 'LIKE', 'value' => $_REQUEST['practice'] )
    					// ... Add as many other criteria as you need
    				)
    			);
    
    			query_posts( $searchArgs );

Topic Closed

This topic has been closed to new replies.

About this Topic