2

I am using folowing code to store posts ids into array:

<?php
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => 5,
        'orderby' => 'date',
        'order' => 'desc');
$id = array();
$counter = 1;       
$products = new WP_Query( $args );
if ( $products->have_posts() ) : while ( $products->have_posts() ) : $products->the_post();
    $id[$counter] = get_the_id();
    //custom_shop_array_create($product, $counter);
    $counter++;
endwhile;
endif;
?>

However it doesnt work because if I put print_r($id) after endif it only prints id of last post. Where am I making mistake?

Thanks in forward

4

Try to replace

$id[$counter] = get_the_id();

with

array_push( $id, get_the_ID() );

to collect the post id's into the $id array.

Update:

If you also use $ids instead of $id:

    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => 5,
        'orderby' => 'date',
        'order' => 'desc');
$ids = array();
$products = new WP_Query( $args );
if ( $products->have_posts() ) : 
    while ( $products->have_posts() ) : $products->the_post();
       array_push( $ids, get_the_ID() );
    endwhile;
endif;
  • not working. It is still acting like $id is not an array. – horin Jul 22 '13 at 13:53
  • what if you use $ids instead of $id ? – birgire Jul 22 '13 at 14:05
  • working ... there must have been some sort of interference with wordpress variables – horin Jul 22 '13 at 14:09
  • ok great, must have been some interference, you could try var_dump($id) check it before and after. – birgire Jul 22 '13 at 14:11
1

While @birgire's answer solves the problem, it does not explain it. $id[$counter] = get_the_id(); should work but in this case triggers a Warning that you cannot use a scalar value as an array. Why?

the_post runs setup_postdata, which sets $id to the post ID, overwriting your $id and turning it into an integer. You can see that by adding var_dump after the_post(), like this:

$products->the_post();
var_dump($id);

Beyond that, your code is overly complex. You don't need the counter (and if you did you already have $products->current_post) and you don't need any particular function to push items onto the array. All you really need to do is use a variable that WordPress isn't already using, which is what makes birgire's solution work.

$args = array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'ignore_sticky_posts'   => 1,
  'posts_per_page' => 5,
  'orderby' => 'date',
  'order' => 'desc'
);

$ids = array();  
$products = new WP_Query( $args );
if ( $products->have_posts() ) : 
  while ( $products->have_posts() ) : 
    $products->the_post();
    $ids[] = $post->ID;
    //custom_shop_array_create($product, $counter);
  endwhile;
endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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