• Resolved antonop4u

    (@antonop4u)


    Hi I’m working on a new plugin, and I’m using the WP_query.
    I checked the documentation and it’s not really clear if a query like the one below should work.
    on my plugin it doesn’t work and most of all it stalls the website. Any suggestion on how to make it work?

    $query = new WP_Query( array (
    			'posts_per_page' => -1,
    			'post_type' 	=>  $my_post_type,
    			'orderby' 		=> 'rand',
    			'meta_query' 	=> array(
    						'relation'	=> 'OR',
    						array(
    									'relation'	=> 'AND',		
    										array(
    											'key' 		=> 'first_key',
    											'compare' => '=',
    											'value'		=> 'first_value'
    										),
    										array(
    											'key' 		=> 'second_key',
    											'compare' => '>=',
    											'value'		=> 'second_value',
    											'type' 		=> 'numeric'
    										),
    										array(
    											'key' 		=> 'third_key',
    											'compare' => '<=',
    											'value'		=> 'third_value',
    											'type' 		=> 'numeric'
    										),
    										array(
    											'key' 		=> 'fourth_key',
    											'compare' => '=',
    											'value'		=> 'fourth_value'
    										),
    							),
    							array(
    										'relation'	=> 'AND',
    										array(
    											'key' 		=> 'fifth_key',
    											'compare' => '=',
    											'value'		=> 'fifth_value'
    										),
    										array(
    											'key' 		=> 'sixth_key',
    											'compare' => '>=',
    											'value'		=> 'sixth_value',
    											'type' 		=> 'numeric'
    										),
    										array(
    											'key' 		=> 'sevnth_key',
    											'compare' => '<=',
    											'value'		=> 'sevnth_value',
    											'type' 		=> 'numeric'
    										),
    										array(
    											'key' 		=> 'eighth_key',
    											'compare' => '=',
    											'value'		=> 'eighth_value'
    										),
    							),
    				),
    ) );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Indexing. That’s about all that you’re going to have help. When you join the wp_postmeta table 8 times like your query does, it results in very slow queries because the standard indexing isn’t quite as good as it should be.

    One thing to be mindful of is that with a query like that, no matter what you do it will be slow.

    The only thing that might help a bit more is if you can write your own SQL query for this. That will help a bit because you’ll know exactly what is happening and what indexing is needed. It probably will never be blazingly fast, but it might help a little bit.

    Thread Starter antonop4u

    (@antonop4u)

    do you see any problem if i make a query less selective (with just two criterias) getting out more posts, and then I filter them using the code? do I slow down the process or use more memory or cpu … ?

    Dion

    (@diondesigns)

    As long as you insist on using meta_query, your query will be slow no matter what you do.

    If you really want this to work, you should create your own (properly-indexed) DB table of this data as opposed to putting them in the _postmeta table. You could then manually query the database using a JOIN of your custom table against the _posts table. I could easily see this new method being thousands of times faster than the method you’re currently using.

    Thread Starter antonop4u

    (@antonop4u)

    perfect!
    thanks you very much.

    • This reply was modified 5 years, 9 months ago by antonop4u.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query stalls the website’ is closed to new replies.