meta_query in pre_get_posts (4 posts)

  1. 5amsan
    Posted 2 years 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.


    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
    Lead Developer
    Posted 2 years 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
    Posted 2 years 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
    Posted 2 years ago #

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

    Could I try to fix the query with some filter ?

Topic Closed

This topic has been closed to new replies.

About this Topic