I am using query_posts( $args )
to filter the Loop.
I want to filter posts based on their meta_value
“vote”, sometimes smaller than, sometimes equals and so on….
I definitly want to use the query_posts()
function and pass my filter through $args
!
I don’t want to use add_filter('posts_where', 'filter_where');
and then add an AND
statement to the query.
I want to use the given functionality of WordPress to filter posts with meta_key
, meta_value
and meta_compare
like this:
$args = array( 'meta_key'=>'vote', 'meta_compare'=>'>=', 'meta_value'=>5, 'posts_per_page'=>100 ) )
query_posts( $args );
The result of this is:
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type="post" AND (wp_posts.post_status="publish" OR wp_posts.post_status="private") AND wp_postmeta.meta_key = 'vote' AND wp_postmeta.meta_value >= '5' GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 100
The problem of that is:
wp_postmeta.meta_value >= '5'
It should be:
wp_postmeta.meta_value >= 5
Then it would work fine.
I don’t understand why WordPress adds quotes.
I’m using the predefined parameter from WordPress (<
, >
, <=
, >=
) and it’s obvious that this will only work with numbers and not strings which would need to be in quotes.
The documentation says:
Returns posts with custom field key of
‘miles’ with a custom field value that
is LESS THAN OR EQUAL TO 22
query_posts('meta_key=miles&meta_compare=<=&meta_value=22');