简体   繁体   中英

Custom Select Query by meta key and cat id

I have this custom query that get the expiry date of the post and orders post by that. How do I amend it to only list from a certain category id eg '20'

$querystr = "
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = 'es_ape_expiry'
    ORDER BY wpostmeta.meta_value ASC
    ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

Many Thanks !

Try following query and replace $cat_id with 20

SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'es_ape_expiry' 
AND tt.taxonomy = 'category'
AND t.term_id = $cat_id
ORDER BY wpostmeta.meta_value ASC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM