Is there a better, more effectient way to write this get_post query?

I have multiple custom post types registered.
I am using the “Advanced custom fields” plugin and using the “Relationship” field type.

My query at the moment looks like this, but it seems to be rather expensive so i am looking for a better way to write it. The more arrays I add to it the longer it takes to complete the query. The three that i have is not so bad, but another post type that i have has around 13. It takes over a minute to complete that query. That is just too long.

Take a look at this code:



$leaders = get_posts(array(
  'post_type' => 'leadership',
  'post_status' => 'publish',
  'meta_query' => array(
		'relation' => 'OR',
	  array(
		  'key' => 'expertise_relationship', // name of custom field
		  'value' => '"40"', // exact match for Government
		  'compare' => 'LIKE'
	  ),
		array(
		  'key' => 'expertise_relationship', // name of custom field
		  'value' => '"39"', // exact match for Social Marketing & Advocacy
		  'compare' => 'LIKE'
	  ),
		array(
		  'key' => 'expertise_relationship', // name of custom field
		  'value' => '"38"', // exact match for Healthcare
		  'compare' => 'LIKE'
	  )
  )
));


More or less what it is doing is looking in the custom post type “leadership” for any posts with a expertise relationship of goverment, social marketing, and healthcare.

Is there a way write this shorter or differently in order to optimize the query?

I’m no WP expert by any means, but I do know a little something about databases.
If you’re looking for an exact match, you should not be using ‘LIKE’, but ‘==’, which should be a lot faster.
Other than that, it depends on which tables there are, how large they are, how WP is querying them, etc, which I really couldn’t tell.

Currently, the database is very small. The only thing in it are the things i am connecting together. I will try what you suggested and attempt to use the “equal” instead of “LIKE”. I knew “LIKE” was expensive. I wasnt sure just how expensive and if there was just a better way to write my query.

Thanks for the suggestion. I’ll try that out and report back.

I just gave that whirl and it took 88.888 seconds to load the page. That’s pretty slow. This is what i tried.



$leaders = get_posts(array(
  'post_type' => 'leadership',
  'post_status' => 'publish',
  'meta_query' => array(
		'relation' => 'OR',
	  array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"42"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"43"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"44"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"45"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"46"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"47"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"48"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"49"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"50"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"51"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"52"', // exact match
		  'compare' => '='
	  ),
		array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => '"53"', // exact match
		  'compare' => '='
	  )
  )
));


Currently there are no connections to be made so the results should have been empty, which is was. Not sure why this takes so long to query.

So, i’m betting there is probably just a better way to write this query.

From what I can gather from the information out there, this should do the same and be -hopefully, depending on how WP processes this internally- faster.


$leaders = get_posts(array(
  'post_type' => 'leadership',
  'post_status' => 'publish',
  'meta_query' => array(
	  array(
		  'key' => 'capabilities_relationship', // name of custom field
		  'value' => (42, 53), // min and max values
		  'compare' => 'BETWEEN', // search between min and max values
		  'type' => 'NUMERIC' // we're looking for integer values
	  ),
  )
));