• Running the latest version of WordPress and I’m trying to do a nested meta_query. The query requires the entry to have a STARTDATE greater than today and at least one WIDGET entry matching an ID (382 below).

    As far as I can see, my ARGS are correct, but the resulting query comes out as AND instead of OR. Running the query directly in PHPMyAdmin after converting the AND to OR produces the correct results.

    Any help is appreciated.

    This is the output of print_r on $args being passed to WP_Query:

    Array
    (
        [post_type] => jr_events
        [post_status] => Array
            (
                [0] => publish
                [1] => draft
            )
    
        [meta_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [key] => _jr_start_date
                        [value] => 1441594679
                        [compare] => >=
                    )
    
                [1] => Array
                    (
                        [relation] => OR
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [key] => _jr_widget_0
                                        [value] => 382
                                        [type] => NUMERIC
                                    )
    
                                [1] => Array
                                    (
                                        [key] => _jr_widget_64
                                        [value] => 382
                                        [type] => NUMERIC
                                    )
    
                                [2] => Array
                                    (
                                        [key] => _jr_widget_65
                                        [value] => 382
                                        [type] => NUMERIC
                                    )
    
                                [3] => Array
                                    (
                                        [key] => _jr_widget_66
                                        [value] => 382
                                        [type] => NUMERIC
                                    )
    
                            )
    
                    )
    
            )
    
    )

    The SQL that gets output is as follows:

    SELECT SQL_CALC_FOUND_ROWS wp_10_posts.id
    FROM   wp_10_posts
           INNER JOIN wp_10_postmeta
                   ON ( wp_10_posts.id = wp_10_postmeta.post_id )
           INNER JOIN wp_10_postmeta AS mt1
                   ON ( wp_10_posts.id = mt1.post_id )
           INNER JOIN wp_10_postmeta AS mt2
                   ON ( wp_10_posts.id = mt2.post_id )
           INNER JOIN wp_10_postmeta AS mt3
                   ON ( wp_10_posts.id = mt3.post_id )
           INNER JOIN wp_10_postmeta AS mt4
                   ON ( wp_10_posts.id = mt4.post_id )
    WHERE  1 = 1
           AND ( ( wp_10_postmeta.meta_key = '_jr_start_date'
                   AND Cast(wp_10_postmeta.meta_value AS CHAR) >= '1441594679' )
                 AND (( ( mt1.meta_key = '_jr_widget_0'
                          AND Cast(mt1.meta_value AS signed) = '382' )
                        AND ( mt2.meta_key = '_jr_widget_64'
                              AND Cast(mt2.meta_value AS signed) = '382' )
                        AND ( mt3.meta_key = '_jr_widget_65'
                              AND Cast(mt3.meta_value AS signed) = '382' )
                        AND ( mt4.meta_key = '_jr_widget_66'
                              AND Cast(mt4.meta_value AS signed) = '382' ) )) )
           AND wp_10_posts.post_type = 'jr_events'
           AND (( wp_10_posts.post_status = 'publish'
                   OR wp_10_posts.post_status = 'draft' ))
    GROUP  BY wp_10_posts.id
    ORDER  BY wp_10_posts.post_date DESC
    LIMIT  0, 10

Viewing 4 replies - 1 through 4 (of 4 total)
  • arend

    (@arenddeboer)

    Did you find a solution ?
    I also need a meta query which has to meet a certain condition, OR the post type is nav_menu_item (or no menu will be displayed as it is a main query)

    @arenddeboer: This is not your topic. It is also 9 months old and references an old version of WordPress.If you require assistance then, as per the Forum Welcome, please post your own topic.

    arend

    (@arenddeboer)

    Ok thanks, even though I do not believe having multiple posts covering the same (non-answered) topic is beneficial for the community.

    @arenddeboer: These forums have guidelines. Please read them. As you will see, one of these guidelines – entitled Where to Post – specifically states:

    Unless you are using the same version of WordPress on the same physical server hosted by the same hosts with the same plugins, theme & configurations as the original poster, do not post in someone else’s thread. Start your own topic.

    Please abide by this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query nested meta_query with OR switching to AND?’ is closed to new replies.