0

I am going to make a "favourite" posts box, but the article id's are stored in another DB so I only get an array of IDs (which are relevant to the WP DB).

I think it could be done with get_posts() or simply just with WP_Query, however as I am surfing through the codex I cant find solution to my problem - you can obviously select posts by multiple tags, authors,... but I cant find something similar for just IDs.

I wouldnt even mind if the posts werent ordered. Mainly I just want to get a bunch of posts in one (WP)Query providing only posts' ids.

Any Ideas? Thanks :)

2 Answers 2

3

You could use post__in as a parameter in your WP_Query, like:

$favourite_posts = new WP_Query(
    array( 
        'post__in' => array( 111, 222, 333 ),
        'orderby' => 'ID'
    ) 
);
5
  • It solves the main problem. However - one more question - How to order by the position of ids in provided array? You know like 'post__in' => array(8,1,4,2) would return posts in order like 8,1,4,2 - or is that by default with using post__in?
    – jave.web
    Jan 29, 2014 at 5:29
  • 1
    The custom order like 8,1,4,2, cannot be done with WP_Query by using orderby parameter, you have to select the posts first, then order them in PHP, maybe with a custom order function, something like that.
    – 1fixdotio
    Jan 29, 2014 at 5:37
  • Another funny thing - if the post__in's array() is empty it returns all the posts
    – jave.web
    Jan 29, 2014 at 5:49
  • Yes, it did! I've encountered that before.
    – 1fixdotio
    Jan 29, 2014 at 5:54
  • 1
    So to prevent extra posts I ended up with: WP_Query(array( 'post__in' => $favourite_posts, 'cat' => 1, 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 3 ))
    – jave.web
    Jan 29, 2014 at 6:08
3

To Maintain the Order as by Given IDs

Since 3.5 we have option called orderby => post__in

Preserve post ID order given in the post__in array (available since Version 3.5).

$ordered_posts = new WP_Query(
    array( 
        'post__in' => array( 21, 5, 33 ),
        'orderby' => 'post__in'
    ) 
);
3
  • Do you realize this makes as many SQL queries as there are fetched posts? :-) get_posts/WP_Query should loot them at once ...
    – jave.web
    Oct 24, 2016 at 9:58
  • You are right it is n+1 queries scenario. Thanks for the heads up, in my case it was for given three posts I wanted to show. But latest WP have another ordering built-in so I am going to update my answer with that. Thanks again for your kind feedback! Oct 24, 2016 at 12:41
  • 1
    This is not what I wanted, but I will give this a +1 because it is an interesting way of abstractly doing custom order-ing :-) BTW: Be aware of queries*n, especially in WP stack, forums and other WP material this is often overlooked and causes trouble when used within more complex / larger websites :)
    – jave.web
    Oct 26, 2016 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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