• I’m going off of the following documentation:
    http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    What I need to do is query posts that either have content or titles that conform to a search term, or else postsmeta values that conform to same. However, when I look at the output query, it appears to be searching content AND meta, if I’m reading the query right:

    [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1  AND (((wp_posts.post_title LIKE '%hello%') OR (wp_posts.post_content LIKE '%hello%')))  AND wp_posts.post_type IN ('post', 'page', 'attachment', 'obit') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'obit_name' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%hello%')
    OR  (mt1.meta_key = 'obit_name' AND CAST(mt1.meta_value AS CHAR) LIKE '%hello%') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

    This query returns no results unless a value exists in both content and a meta key. However, if I eliminate the segment of the above statement that deals with the post content, “AND (((wp_posts.post_title LIKE ‘%hello%’) OR (wp_posts.post_content LIKE ‘%hello%’)))”, I get results with the query term in the meta.

    I am unsure how to get myself around this problem. I can create a custom search box, perhaps, that doesn’t send the s parameter? Or is there a clever way to get around this problem? Preferably, I would like a search that is flexible enough to search both standard content values and meta, so that users can get the widest variety of results.

    For reference, here is my query modifying code:

    /*
    	// Set our custom obituary search params, when needed:
    	*/
    	function search_query( $query ) {
    		if ( !is_admin() && $query->is_search ) {
    			$time	= &strtotime($query->query_vars['s']);
    			$query->set('search_terms', array());
    			$query->set('meta_query', array(
    				'relation'	=> 'OR',
    				array(
    					'key'		=> 'obit_name',
    					'value'		=> $query->query_vars['s'],
    					'compare'	=> 'LIKE'
    				),
    				array(
    					'key'		=> 'obit_name',
    					'value'		=> $query->query_vars['s'],
    					'compare'	=> 'LIKE'
    				),
    
    			));
    			// $query->set('post_type', 'obit'); // optional
    			// print_r($query->query_vars);
    		};
    	}
  • The topic ‘WP_Query content OR meta’ is closed to new replies.