Support » Plugins and Hacks » [Resolved] How to extract post ids from an array?

[Resolved] How to extract post ids from an array?

  • Hi all,

    What I’m trying to do should be really simple, yet I’ve spent the whole morning trawling through the forums with no joy! I’ve created an array:

    <?php $latest_category_posts = new WP_Query(array(‘category__in’=>array(‘7’), ‘tag__not_in’=>array(‘11,13,15’), ‘post__not_in’=>array($mypost), ‘showposts’=>’4’)); ?>

    which obviously holds numerous elements. How can I further refine my varible $latest_category_posts so that it only holds the post ids, i.e $latest_category_posts = $latest_category_posts(with just the ids!!)?

    It seems as though it should be really easy yet I can’t figure it out at all!!

    Any help will be greatly appreciated! Cheers.

Viewing 9 replies - 1 through 9 (of 9 total)
  • this after your query

    //create array of post ids
    $postids = array();
    foreach ($latest_category_posts as $catpost) {
    //echo "<pre>"; print_r($postids); echo "</pre>";

    Hi Michael,

    Thanks for your speedy reply! I popped your code after the query and got this result:

    [0] =>
    [1] =>
    [2] =>
    [3] =>
    [4] =>
    [5] => 555
    [6] =>
    [7] =>
    [8] =>
    [9] =>
    [10] =>
    [11] =>
    [12] =>
    [13] =>
    [14] =>
    [15] =>
    [16] =>
    [17] =>
    [18] =>
    [19] =>
    [20] =>
    [21] =>
    [22] =>
    [23] =>
    [24] =>
    [25] =>
    [26] =>
    [27] =>
    [28] =>
    [29] =>
    [30] =>
    [31] =>
    [32] =>
    [33] =>
    [34] =>
    [35] =>
    [36] =>
    [37] =>
    [38] =>
    [39] =>
    [40] =>

    The id of 555 is correct as it is the id of the first post (out of the four) that should be in the array. The other three are missing though! The reason I want this is so that I can exclude the posts in the array from another loop using the post__not_in parameter.

    Once again, thanks for you help!

    Well this in my index.php shows all posts that I have when echoing that array:

    //create array of post ids
    $postids = array();
    $latest_category_posts = get_posts('showposts=-1');
    foreach ($latest_category_posts as $catpost) {
    echo "<pre>"; print_r($postids); echo "</pre>";


    Should have mentioned that this is on single.php! I have multiple loops on this page all working fine – I just need to avoid duplication in two of the loops. I realise that single.php is meant for exactly that but I’m sure there’s a way of achiveing this! Thanks for your help!


    I’ve got very close with:

    $latest_category_posts = get_posts(array(‘category__in’=>array(‘7’), ‘tag__not_in’=>array(‘11,13,15’), ‘post__not_in’=>array($mypost), ‘showposts’=>’4’));
    foreach ($latest_category_posts as $catpost) {
    $postids[]=$catpost->ID; $mypostids = implode (‘,’, $postids); ?>

    When echoed, this code outputs the correct values separated by a comma: 555,553,152,46.

    However, for it to work with the post__not_in parameter I need single quotation marks around each ID: ‘555’,’553′,’152′,’46’

    Does anyone know how to achieve this?!

    Cheers for any help!!

    I’ve just checked the post__not_in function and it appears to accept post ids without single quotation marks, so that’s not the issue.

    When I put in:


    it excluds the posts. But when I replace that with:


    it doesn’t work, even though $mypostids printed is 555,553,152,46 exactly.

    what about just 'post__not_in'=>$mypostids

    Figured it out (thanks to Michael’s comments in another thread).

    In case anyone needs to do similar, the final code to define the array is:

    $latest_category_posts = get_posts(array(‘category__in’=>array(‘7’), ‘tag__not_in’=>array(‘11,13,15’), ‘post__not_in’=>array($mypost), ‘showposts’=>’4’));
    foreach ($latest_category_posts as $catpost) {

    The code to exclude the array from another loop:

    ‘post__not_in’ => $postids,

    rather than the code I was using before, which only excluded the first post in the array.

    Should have refreshed my screen before posting! Cheers for your help Michael.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] How to extract post ids from an array?’ is closed to new replies.
Skip to toolbar