I’ve a customized put up sort ‘occasion’, a customized meta discipline ‘event_date’ and a customized taxonomy ‘places’.
I wish to question $wpdb
to retrieve posts on this means
- posts should be of the ‘occasion’ put up sort
- ‘occasions’ should be related to a sure
$location
time period inside the ‘places’ taxonomy - outcomes need to be ordered by the ‘event_date’ customized meta worth (which is definitely a date in
yymmdd
format), in contrast with at the moment’s current date
I am making an attempt with the next question parameters (I can get the $location
worth – both ID or slug – accurately and move to this question):
SELECT $wpdb->posts.*
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->phrases ON($wpdb->term_taxonomy.term_id = $wpdb->phrases.term_id)
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->phrases.term_id = $location
AND $wpdb->term_taxonomy.taxonomy = 'places'
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_type = 'occasion'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->postmeta.meta_key = 'event_date'
AND $wpdb->postmeta.meta_value > NOW()
ORDER BY $wpdb->postmeta.meta_value ASC
LIMIT $numberofposts
the question isn’t working; if I take away the JOIN half and the taxonomy half, it’ll work, ordering all the outcomes by evaluating ‘event_date’ meta with NOW()
date.
I suppose I am doing it unsuitable with the taxonomy half…
As suggested, I attempted to do a WP_Query
somewhat than a $wpdb
question:
$args = array(
'post_type' => 'occasion',
'tax_query' => array(
array(
'taxonomy' => 'places',
'discipline' => 'id',
'phrases' => $location // location time period id
)
),
'meta_key' => 'event_date', // this meta discipline shops occasion date in yymmdd format
'meta_value' => $at the moment, // this could be at the moment's date in yymmdd format
'meta_compare' => '>=',
'posts_per_page' => $numberofposts, // this variable shops the variety of posts I wish to get
'orderby' => 'meta_value_num'
);
Nonetheless, on this latter case, the question will return ALL the posts beneath the specificed post_type, no matter every other specification inside $args
together with the sorting order
I’ve tried utilizing meta_query
as a substitute of meta_key
however outcome does not change