Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to display list of all posts under a couple of categories and some individual posts. If I use the following code, it does not return anything, as it looks for those individual posts in the categories.

query_posts( array ( 'category__in' => array( 28, 34 ), 'post__in' => array( 8200, 3581, 38, 1562, 7613 )) ); 

How can I get list of all posts under a couple of categories plus some individual posts found under other categories?

share|improve this question

2 Answers 2

I decided to have two separate loops, one for posts from category and another for selected posts. But I would like others to provide their solutions.

share|improve this answer

The problem here is that WP_Query requires the posts returned to be both in your lists of post IDs and in one of your categories. The SQL query generated by your code looks something like this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND wp_posts.ID IN (8200,3581,38,1562,7613) AND ( wp_term_relationships.term_taxonomy_id IN (28,34) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

While what you would need in order to run your query is this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND (( wp_term_relationships.term_taxonomy_id IN (28,34) ) OR wp_posts.ID IN (8200,3581,38,1562,7613)) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

In my opinion the most elegant solution would be to remove the posts__in keyword from your query, and inject the proper SQL (that is the wrapped OR distinction between your category and post queries), so that your final SQL statement matches the one above, like so:

add_filter( 'posts_where' , 'my_posts_where' );
function my_posts_where( $where ) {
    $where = ... // code to inject the OR statement and wrap 
    return $where;
}
query_posts( array ( 'category__in' => array( 28, 34 )) ); 
remove_filter( 'posts_where' , 'my_posts_where' );

That probably involves a fair bit of string mangling, so an easier solution might be to simply query $wpdb directly like this:

global $wpdb;
$sql = $wpdb->prepare("SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.ID FROM $wpdb->posts INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) WHERE 1=1 AND (( $wpdb->term_relationships.term_taxonomy_id IN (28,34) ) OR $wpdb->posts.ID IN (8200,3581,38,1562,7613)) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish') GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 0, 10");
$posts = $wpdb->query($sql);

Off course, this solution will return a posts list, and not an iterable WP_Query object. If you need to use this in a template query the posts_where filter function described above is probably the best solution.

share|improve this answer
    
Good solution. Thank you. But I decided to have two separate loops, one for posts from category and another for selected posts. –  Debiprasad Jan 29 '13 at 9:30
1  
If you are going to display the posts in two separate sections/lists anyway that would definitely be the easiest solution. Could even work if you need to mix them up by manually sorting them by date or whatever. –  Simon Jan 29 '13 at 9:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.