• I’m trying to show only certain categories on my posts page. I’ve followed https://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
    using this query:

    <?php
    
     $querystr = "
        SELECT * FROM $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)
    	WHERE $wpdb->term_taxonomy.term_id IN (1)
    	AND $wpdb->term_taxonomy.taxonomy = 'category'
    	AND $wpdb->posts.post_status = 'publish'
    	ORDER BY $wpdb->postmeta.meta_value ASC
    	 ";
    
     $pageposts = $wpdb->get_results($querystr, OBJECT); ?>

    In my wordpress site I have 5 categories existing. However, if I put 1 into the query the query returns all posts, if I put anything else it returns 0 posts. So probably I’ve missed something in understanding the query (not a php expert…). Can someone help?

The topic ‘Custom query for posts in wordpress 4.5.3’ is closed to new replies.