WordPress.org

Support

Support » How-To and Troubleshooting » meta_query in pre_get_posts

meta_query in pre_get_posts

  • Hi all,
    and thanks for reading.

    I’m using a pre_get_posts filter to customize a search.
    The hilighted part is the tough one.
    It seems to cause $query to return a SQL request containing the bad “WHERE … AND 0=1 …” string.

    I can’t figure out why is this.

    http://pastebin.com/qktr1pP0

    Here’s the request string

    SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts
    INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
    WHERE 1=1
    AND 0=1
    AND wp_posts.post_type = 'lot'
    AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
    AND (wp_postmeta.meta_key = 'lot_prix'
    	AND (mt1.meta_key = 'lot_zip' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '31000' AND '31999')
    )
    GROUP BY wp_posts.ID
    ORDER BY wp_postmeta.meta_value+0 DESC
    LIMIT 0, 10

Viewing 3 replies - 1 through 3 (of 3 total)
  • The 0=1 isn’t coming from the Meta query, It’ll be coming from the Taxonomy query.

    When WordPress determines (for whatever reason) that a Taxonomy query will have 0 results, it uses 0=1 to cause no posts to be selected.

    Take a look at lines 12~26 and check to see if the terms you’re requesting should actually have some posts or not.

    Thank you for this Dion.
    I’m sure it will return something since it returns results with another meta query (ie. if I use a 5 digit number for “lot_zip”).
    The 0=1 shows up when the hilighted condition is used.

    Well, I’m still stuck on this. Any idea to bypass this issue ?

    Could I try to fix the query with some filter ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘meta_query in pre_get_posts’ is closed to new replies.