WordPress.org

Ready to get started?Download WordPress

Forums

meta_query in pre_get_posts (4 posts)

  1. 5amsan
    Member
    Posted 10 months ago #

    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
  2. Dion Hulse
    WordPress Dev
    Posted 10 months ago #

    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.

  3. 5amsan
    Member
    Posted 10 months ago #

    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.

  4. 5amsan
    Member
    Posted 9 months ago #

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

    Could I try to fix the query with some filter ?

Reply

You must log in to post.

About this Topic