• Resolved squasher

    (@squasher)


    Hi,

    I’ve got a meta query with two queries and a AND relation. But somehow it’s not working. I discovered only the first part is executed. So, I must have an error somewhere.

    This is my code:

    
    $today = getdate();
    $tax_query = array('relation' => 'OR');
    $post_ids = array();
     $args = array( 'post_type' => 'wmw_activities', 'posts_per_page' => 6 ,
                   'relation' => 'AND',
                   'orderby' => 'rand',
                   'order'   => 'rand',
                   'meta_key' => '_wmw_mbe_date',
                   'meta_key' => '_wmw_mbe_sticky',
                   'meta_query' => array(
                                    'relation' => 'AND',
                                    'key' => '_wmw_mbe_sticky',
                                    'value' => 'sticky',
                                    'compare' => '=' ),
                                    array(
                                    'key' => '_wmw_mbe_date',
                                    'value' => date("Y-m-d"),
                                    'compare' => '>' ),
                                    'type' => 'DATE',
                   'category__in' => Array
                           (
                             1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
                             ),
                        );
                   $loop = new WP_Query( $args );
                   while ( $loop->have_posts() ) : $loop->the_post();
                        //ad ids to array to filter out in next query
                        $post_ids[] = get_the_ID();
                        //get the styling
                        wmw_results_layout();
                   endwhile;
    

    So it’s about the meta_query sticky and the date-part. With the code above the date is not taken into account. If I switch the date-part above the sticky-part, the date filter is correct, but the sticky part is ignored. And v.v.

    What am I doing wrong here?

    Thanks for any help!

    • This topic was modified 6 years, 9 months ago by squasher.
Viewing 5 replies - 1 through 5 (of 5 total)
  • There are a few things wrong here.

    1) In a normal WP_Query, you can’t have relation => 'AND' outside meta_query or tax_query, so your relation after posts_per_page is pointless/may be messing things up.

    2) Meta Queries and Tax Queries need nested arrays, like this:

    'meta_query' => array(
    	'relation' => 'AND',
    	array(
    		'key'		=> 'metaquery1',
    		'compare'	=> 'EXISTS',
    	),
    	array(
    		'key'		=> 'metaquery2',
    		'compare'	=> 'EXISTS',
    	),
    ),

    If you compare that to what you currently have you’ll notice that your first meta_query statement ( besides the relation ) is not in array where it does need to be. For further examples see WP_Query meta_query. Specifically the “Multiple Custom Field Handling” section.

    It’s formatted incorrectly. A meta query needs to be formatted like this:

    'meta_query' => array(
    	'relation' => 'AND',
    	'meta_query_1' => array(),
    	'meta_query_2' => array(),
    )
    

    This is what your WP_Query arguments look like when properly indented:

    'post_type'      => 'wmw_activities', 
    'posts_per_page' => 6 ,
    'relation'       => 'AND',
    'orderby'        => 'rand',
    'order'          => 'rand',
    'meta_key'       => '_wmw_mbe_date',
    'meta_key'       => '_wmw_mbe_sticky',
    'meta_query'     => array(
    	'relation'=> 'AND',
    	'key'     => '_wmw_mbe_sticky',
    	'value'   => 'sticky',
    	'compare' => '=' 
    ),
    array(
    	'key'     => '_wmw_mbe_date',
    	'value'   => date("Y-m-d"),
    	'compare' => '>' 
    ),
    'type'           => 'DATE',
    'category__in'   => Array (
    	1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
    ),
    

    It’s sort of a mess, with lots of arguments in the wrong place. This is why it’s important to format your code.

    You have the following issues with your query:

    • You have a relation argument in the top-level of the query args. This isn’t going to do anything.
    • order and orderby are both set to rand. rand is only a valid value for orderby, and if you’re doing that order won’t doanything and can be omitted.
    • You have two meta_key arguments. You can only have one meta_key argument, and you’d only use it if you were querying one custom field or sorting by a custom field.
    • Your meta query is formatted incorrectly. You have relation on the same level as the arguments for the first meta query, and then the arguments for the second query have been added into an array as an argument on the top level of the query.
    • The type argument of the 2nd meta query isn’t in the array of arguments for the meta query.
    • Array in the category__in argument is incorrectly capitalised and has a gap between it and the parentheses.

    Taking all that into account, your WP_Query arguments should look like this:

    'post_type'      => 'wmw_activities', 
    'posts_per_page' => 6 ,
    'orderby'        => 'rand',
    'category__in'   => array(
    	1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
    ),
    'meta_query'     => array(
    	'relation' => 'AND',
    	array(
    		'key'     => '_wmw_mbe_sticky',
    		'value'   => 'sticky',
    		'compare' => '=',
    	),
    	array(
    		'key'     => '_wmw_mbe_date',
    		'value'   => date( 'Y-m-d' ),
    		'compare' => '>' ),
    		'type'    => 'DATE',
    	),
    
    
    Thread Starter squasher

    (@squasher)

    Thanks @howdy_mcgee and @jakept!

    And especially Jacob for the long answer. I’ve really learned a lot from your answer. I’ve used your feedback to change my code and it works as intended now. And is nicely formatted as well ;). Very glad with that!

    There’s one small error/typo in your code: the “)” before ‘type’ may be deleted, I believe?

    Thanks again for your help!

    Moderator bcworkz

    (@bcworkz)

    In Jake’s last example? Yes, it does not belong. An unfortunate carry over from your erroneous code ?? No biggie.

    As you probably surmised, Jake’s last example is also lacking a closing ), for the overall ‘meta_query’ array, and the entire snippet is an excerpt of the overall arguments array, of which both the opening array( and closing ) and other arguments are missing. Such is the nature of snippets, blocks of code often do not balance out their delimiters. It’s up to you to integrate the snippet and ensure the delimiters balance.

    @squasher Yes, you’re right, sorry. I can’t edit it anymore but for posterity all together should be this:

    'post_type'      => 'wmw_activities', 
    'posts_per_page' => 6 ,
    'orderby'        => 'rand',
    'category__in'   => array(
    	1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
    ),
    'meta_query'     => array(
    	'relation' => 'AND',
    	array(
    		'key'     => '_wmw_mbe_sticky',
    		'value'   => 'sticky',
    		'compare' => '=',
    	),
    	array(
    		'key'     => '_wmw_mbe_date',
    		'value'   => date( 'Y-m-d' ),
    		'compare' => '>',
    		'type'    => 'DATE',
    	),
    ),
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘only first part of meta_query works’ is closed to new replies.