We help IT Professionals succeed at work.

custom WP query not searching title or content

jblayney
jblayney asked
on
Hello,

I am coding a custom Wordpress query, I want my keyword field to search post title, content and meta_queries..  The meta_query portion is working fine, but it isn't searching title and content. here is my code

$args = array (
    'post_type'			=>'resources',
    'post_status'       => 'publish',
	'posts_per_page'    => 100,
    'order'      => 'ASC',
    'search'     => '*' . esc_attr( $this_key ) . '*',
    'meta_query' => 
	
           array(
			'relation' => 'OR',
				array(
					'key'     => 'feedback',
					'value'   => $this_key,
					'compare' => 'LIKE'
				),
			array(
				'key'     => 'bfi_steps',
				'value'   => $this_key,
				'compare' => 'LIKE'
			),
			array(
				'key'     => 'agency',
				'value'   => $this_key ,
				'compare' => 'LIKE'
   				 )
		
		
	)

);

Open in new window

Comment
Watch Question

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I want my keyword field to search post title, content and meta_queries
Where in your query are you specifying that it must search title and content?

Author

Commented:
Hello Julian,

here
'search'     => '*' . esc_attr( $this_key ) . '*',

other ways to way to write it are

's'     => '*' . esc_attr( $this_key ) . '*',
's'     => $this_key,

none are providing results
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Have you tried without the '*'

According to the source code a wildcard search is assumed unless the exact property is provided.

https://developer.wordpress.org/reference/classes/wp_query/

Author

Commented:
Hello,

Yes I have tried it all these ways

's' => $this_key,
's' => ''.$this_key.'',
 's'     => '' . esc_attr( $this_key ) . '',
'search' => $this_key,
'search' => ''.$this_key.'',
'search'     => '' . esc_attr( $this_key ) . '',

Open in new window

I am searching a custom post type, but that shouldn't make a difference as i have defined it  with      'post_type'=>'resources',

and if I dump my query, these are results

WP_Query Object
(
    [query] => Array
        (
            [post_type] => resources
            [post_status] => publish
            [posts_per_page] => 100
            [order] => ASC
            [s] => Mock BFI
            [meta_query] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [key] => agency
                            [value] => Mock BFI
                            [compare] => LIKE
                        )

                    [1] => Array
                        (
                            [key] => available_form
                            [value] => Mock BFI
                            [compare] => LIKE
                        )

                )

        )

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
and
's' => "%{this.key}%"

Open in new window


From the source WP should be doing this already but clearly something is not working as it should.

Sometimes trying things that may seem obviously wrong leads to a solution. From our side there is a lot of guesswork as we don't have a working code base to check against.

Author

Commented:
Hi Julian,

this_key is simply catching the variable

$this_key = $_GET['keyword'];

I have made some progress, if I remove the meta_query portion and keep it bare bones it works, but once I add the meta_query, it only searches the meta_query

Author

Commented:
Hello,

So after lots of testing, this is what I'm getting

this search only searches meta_queries and works(but not title or content)  (remove 's' => $this_key)

	$args = array (
		'post_type'			=>'resources',
		'post_status'       => 'publish',
		'posts_per_page'    => 100,
		'order'      		=> 'ASC',
		//'s' => $this_key,
		'meta_query' => 
	 	array(
			'relation' => 'OR',
			array(
				'key'     => 'agency',
				'value'   => $this_key,
				'compare' => 'LIKE'
   				 ),
			array(
				'key'     => 'available_form',
				'value'   => $this_key,
				'compare' => 'LIKE'
			)
		)
	
	);

Open in new window


This searches title and content, but not meta_query (remove array('relation' => 'OR',)

		$args = array (
		'post_type'			=>'resources',
		'post_status'       => 'publish',
		'posts_per_page'    => 100,
		'order'      		=> 'ASC',
		's' => $this_key,
		'meta_query' => 
	 	
			array(
				'key'     => 'agency',
				'value'   => $this_key,
				'compare' => 'LIKE'
   				 ),
			array(
				'key'     => 'available_form',
				'value'   => $this_key,
				'compare' => 'LIKE'
			)
		
	
	);
	
	);

Open in new window



This returns no results at all (everything in)

$args = array (
		'post_type'			=>'resources',
		'post_status'       => 'publish',
		'posts_per_page'    => 100,
		'order'      		=> 'ASC',
		's' => $this_key,
		'meta_query' => 
	 	array(
			'relation' => 'OR',
			array(
				'key'     => 'agency',
				'value'   => $this_key,
				'compare' => 'LIKE'
   				 ),
			array(
				'key'     => 'available_form',
				'value'   => $this_key,
				'compare' => 'LIKE'
			)
		)
	
	);
	

Open in new window

Commented:
So there are quite a few hack like solutions on this page

https://wordpress.stackexchange.com/questions/78649/using-meta-query-meta-query-with-a-search-query-s

none of them work perfect when you start making the query more complicated with category searches

if  (isset($this_cat) &&  ($this_cat != "")  )  {
    $args['tax_query'][] = array('taxonomy' => 'resource_category', 'field' => 'slug', 'terms' => $this_cat );
}

Title, content and categories are most important, so i will remove the meta_query keyword search as it was simply a "nice to have feature"  If I had to make it work I would make extra custom fields for title and content and disable the default ones, make all content use custom fields and then just search that.