Support » Fixing WordPress » WP_QUERY with multiple nested array within meta_query

  • Resolved unicco

    (@unicco)


    WP_QUERY is returning an 504 Gateway Time-out error. I can’t figure out what I’m doing wrong. I want to retrieve all items which have either a price between 1 and 3000 OR 3001 and 6000. The upper “relation” => OR (within meta_query) is causing wordpress to return 504.

    Could someone please help me out, and tell me what I’m doing wrong?

    I’ve constructed my query using https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

    Array
    (
        [post_type] => Array
            (
                [0] => product
            )
    
        [posts_per_page] => 30
        [offset] => 0
        [order] => DESC
        [orderby] => date
        [post_status] => publish
        [ignore_sticky_posts] => 1
        [paged] => 1
        [tax_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [taxonomy] => product_cat
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => moebler
                            )
    
                        [operator] => IN
                    )
    
            )
    
        [meta_query] => Array
            (
                [relation] => OR <----- THIS BUGS
                [0] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => _price
                                [value] => 1
                                [compare] => >=
                                [type] => DECIMAL
                            )
    
                        [1] => Array
                            (
                                [key] => _price
                                [value] => 3000
                                [compare] => <=
                                [type] => DECIMAL
                            )
    
                    )
    
                [1] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => _price
                                [value] => 3001
                                [compare] => >=
                                [type] => DECIMAL
                            )
    
                        [1] => Array
                            (
                                [key] => _price
                                [value] => 6000
                                [compare] => <=
                                [type] => DECIMAL
                            )
    
                    )
    
            )
    
    )
    • This topic was modified 3 years, 5 months ago by unicco.
    • This topic was modified 3 years, 5 months ago by unicco.
Viewing 13 replies - 1 through 13 (of 13 total)
  • @unicco I don’t think you can use ‘=> >=’ for compare the way you are doing.

    Have you set WP_DEBUG to true, this might show some PHP errors you are missing.

    It might also be easier if you past your original PHP code here.

    Actually I’m building the query using Ajax, and printing the query args using var_dump returns an error. So I hooked on a filter, an echo to wp_debug_log instead.

    The docs states: “Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘NOT EXISTS’, ‘REGEXP’, ‘NOT REGEXP’ or ‘RLIKE’. Default value is ‘=’.”, so it should be possible. I could try BETWEEN instead, but I don’t think it would work.

    Oh, sorry I read that wrong, the combination of the array notation ( => ) and the comparison operators threw me.

    Are the meta keys for price definitely ‘_price’ ?

    Yes I’m pretty sure about that. If I only have one nested meta query, it works perfectly. That’s why I know, that the inner atts are correct. If I change the outer relation to AND this works too, but I want to use OR. Saying either the price is between array 1 or its between array 2. I’m just wondering why this is bugging.

    • This reply was modified 3 years, 5 months ago by unicco.

    And a timeout error seems pretty weird aswell… If I put weird/wrong things in my query, I’m use to just getting an empty resultset back. But an timeout error confuses me..

    Can you echo the query that is being run?

    $query = new WP_Query( $args );
    echo $query->request;

    If you run that query directly on the database it might show the errors

    No, because new WP_Query( $args ); returns 504, so I won’t be able to retrieve that prop on that object.

    • This reply was modified 3 years, 5 months ago by unicco.

    Actually I tried to build up my meta_query directly in php. This results in a timeout error aswell. Is this a bug in WP, or what the heck?

    $args[ 'meta_query' ] = array(
    	'relation' => 'OR',
    	array(
    		'relation' => 'AND',
    		array(
    			'key' => '_price',
    			'value' => '1',
    			'compare' => '>=',
    			'type' => 'DECIMAL',
    		),
    		array(
    			'key' => '_price',
    			'value' => '3000',
    			'compare' => '<=',
    			'type' => 'DECIMAL',
    		),
    	),
    	array(
    		'relation' => 'AND',
    		array(
    			'key' => '_price',
    			'value' => '3001',
    			'compare' => '>=',
    			'type' => 'DECIMAL',
    		),
    		array(
    			'key' => '_price',
    			'value' => '6000',
    			'compare' => '<=',
    			'type' => 'DECIMAL',
    		),
    	),
    );

    From error_log (the wp query arg):

    [15-Aug-2017 07:33:16 UTC] Array
    (
        [post_type] => Array
            (
                [0] => product
            )
    
        [posts_per_page] => 30
        [offset] => 0
        [order] => DESC
        [orderby] => date
        [post_status] => publish
        [ignore_sticky_posts] => 1
        [paged] => 1
        [tax_query] => Array
            (
                [relation] => AND
                [0] => 
                [1] => Array
                    (
                        [taxonomy] => product_cat
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => moebler
                            )
    
                        [operator] => IN
                    )
    
            )
    
        [meta_query] => Array
            (
                [relation] => OR
                [0] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => _price
                                [value] => 1
                                [compare] => >=
                                [type] => DECIMAL
                            )
    
                        [1] => Array
                            (
                                [key] => _price
                                [value] => 3000
                                [compare] => <=
                                [type] => DECIMAL
                            )
    
                    )
    
                [1] => Array
                    (
                        [relation] => AND
                        [0] => Array
                            (
                                [key] => _price
                                [value] => 3001
                                [compare] => >=
                                [type] => DECIMAL
                            )
    
                        [1] => Array
                            (
                                [key] => _price
                                [value] => 6000
                                [compare] => <=
                                [type] => DECIMAL
                            )
    
                    )
    
            )
    
    )
    • This reply was modified 3 years, 5 months ago by unicco.

    Alright. I’ve an solution for this. Seems like this should be reported as a bug in WP. I believe the above way to have nested meta-queries are 1:1 with how the docs state it. However, it kept returning an 504 error. I’m not quite sure why, but it seems like WP_QUERY dosen’t translate the arg-query-array correctly.

    Instead I used BETWEEN, without using any nested meta_queries, and this works perfectly.

    $args[ 'meta_query' ] = array(
    	'relation' => 'OR',
    	array(
    		'key' => '_price',
    		'value' => array( 1, 3000 ),
    		'compare' => 'BETWEEN',
    		'type' => 'DECIMAL',
    	),
    	array(
    		'key' => '_price',
    		'value' => array( 3001, 6000 ),
    		'compare' => 'BETWEEN',
    		'type' => 'DECIMAL',
    	),
    );

    https://prnt.sc/g8nuo8

    • This reply was modified 3 years, 5 months ago by unicco.

    I’m not sure if its a bug or just the fact that you can’t nest meta queries in that way, however if you are searching for results between 2 values, the use of the BETWEEN keyword is recommended. Glad you were able to sort it out.

    Please mark this topic as resolved.

    Well if you have that opinion, I’d prefer getting an explanation, why that is so. As far as I can see, the docs outlines, that it’s possible to have nested meta queries, but when using it, it breaks the site. Actually I had a deadlock in the database, and couldn’t access neither front nor backend. I had to restart mysql service before I could access. If you don’t categorize that as a bug, what is it?

    @unicco my apologies, I’ve just setup a test using the structure you have provided

    This is my meta_query

    'meta_query'  => array(
    	'relation' => 'OR',
    	array(
    		'relation' => 'AND',
    		array(
    			'key'     => '_price',
    			'value'   => 1,
    			'compare' => '>=',
    			'type' => 'DECIMAL',
    		),
    		array(
    			'key'     => '_price',
    			'value' <= 3000,
    			'compare' => '<=',
    			'type' => 'DECIMAL',
    		),
    	),
    	array(
    		'relation' => 'AND',
    		array(
    			'key'     => '_price',
    			'value'   => 3001,
    			'compare' => '>=',
    			'type' => 'DECIMAL',
    		),
    		array(
    			'key'     => '_price',
    			'value' <= 6000,
    			'compare' => '<=',
    			'type' => 'DECIMAL',
    		),
    	)
    ),

    And it works returns data on my test bed. So you are correct in that it should be possible and it should work and does in my test.

    Therefore I can only assume there is something else amiss with the query itself or that something is causing hassles elsewhere.

    Apologies for making my earlier assumption, but I find it hard to debug a problem without seeing the original PHP code, hence I had to code it up for myself 😉

    @unicco I am making guesses here, but it could be because you are casting the type as DECIMAL but passing a string for the value?

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘WP_QUERY with multiple nested array within meta_query’ is closed to new replies.