WordPress.org

Ready to get started?Download WordPress

Forums

Meta query in WP_Query not working properly (3 posts)

  1. elitereloaded
    Member
    Posted 1 year ago #

    I'm trying to get posts that match these META query conditions and it's not returning the one and only result that it should be...

    META Code
    $meta_query = array();
    $meta_query[] = array(
    "key" => "venue-min-latitude",
    "value" => $latitude,
    "compare" => "<=",
    "type" => "DECIMAL"
    );

    $meta_query[] = array(
    "key" => "venue-min-longitude",
    "value" => $longitude,
    "compare" => "<=",
    "type" => "DECIMAL"
    );

    $meta_query[] = array(
    "key" => "venue-max-latitude",
    "value" => $latitude,
    "compare" => ">=",
    "type" => "DECIMAL"
    );

    $meta_query[] = array(
    "key" => "venue-max-longitude",
    "value" => $longitude,
    "compare" => ">=",
    "type" => "DECIMAL"
    );

    Generated META
    SELECT wp_4_posts.*
    FROM wp_4_posts
    INNER JOIN wp_4_postmeta ON (wp_4_posts.ID = wp_4_postmeta.post_id) INNER JOIN wp_4_postmeta AS mt1 ON (wp_4_posts.ID = mt1.post_id)
    INNER JOIN wp_4_postmeta AS mt2 ON (wp_4_posts.ID = mt2.post_id)
    INNER JOIN wp_4_postmeta AS mt3 ON (wp_4_posts.ID = mt3.post_id)
    WHERE 1=1 AND wp_4_posts.post_type = 'venue' AND (wp_4_posts.post_status = 'publish')
    AND ( (wp_4_postmeta.meta_key = 'venue-min-latitude' AND CAST(wp_4_postmeta.meta_value AS DECIMAL) <= '47.6582390212')
    AND (mt1.meta_key = 'venue-min-longitude' AND CAST(mt1.meta_value AS DECIMAL) <= '-117.420801473')
    AND (mt2.meta_key = 'venue-max-latitude' AND CAST(mt2.meta_value AS DECIMAL) >= '47.6582390212')
    AND (mt3.meta_key = 'venue-max-longitude' AND CAST(mt3.meta_value AS DECIMAL) >= '-117.420801473') )
    GROUP BY wp_4_posts.ID
    ORDER BY wp_4_posts.post_date
    DESC

    META Table
    http://harrisj.net/wp-content/uploads/wp-meta-query-problem.jpg

  2. elitereloaded
    Member
    Posted 1 year ago #

    UPDATE

    The META query comparisons that are failing are the two below. Together they return no results, but if i remove one then a result is returned (vise versa as well).

    Broken META Query
    SELECT * FROM wp_4_postmeta WHERE ( meta_key = 'venue-min-latitude' AND CAST( meta_value AS DECIMAL ) <= 47.6582390212 ) AND ( meta_key = 'venue-min-longitude' AND CAST( meta_value AS DECIMAL ) <= -117.635970695 )

  3. Robert Simpson
    Member
    Posted 1 year ago #

    I'm experiencing the same problem, except I'm using BETWEEN with the max and min values.

    It seems to be a problem with the negative values.

Topic Closed

This topic has been closed to new replies.

About this Topic