I filtered the post’s in my sidebar by using this code,
$mostlikequerystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->postmeta.meta_value DESC
LIMIT 0 , 10";
This code working perfect , but now i want to add a category filter too..
for this i used $term_id
global $wpdb;
$term_id = get_term_by('slug','trailers');
$term_id->term_id;
echo $term_id;//Prints 12
$mostlikequerystr="SELECT $wpdb->posts.* FROM $wpdb->posts,$wpdb->postmeta
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.$wpdb->taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = $term_id
AND $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'most_liked'
AND $wpdb->term_taxonomy.taxonomy = 'categories'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish')
LIMIT 0 , 10";
$tariler_post = $wpdb->get_results($mostlikequerystr, 'OBJECT');
echo $wpdb->show_errors();
but its not working for me and no error too …
You are missing with conditional operator in your
WHERE
clauseTry this one by adding
AND
If you are using
WPDB
class try to catch the errorsOther way you can use WP’s built in functions