1

Im trying to capture all the id's available in the wp_query after a loop runs and place them into an array. I will use these ID's for a later function.

I tried this, but is there a better way?

        $temp = array();
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
        $temp[] = $wp_query->post->ID ;
    endwhile;
    print_r($temp);

and get this:

Array ( [0] => 7050 [1] => 8227 [2] => 8206 [3] => 8202 [4] => 8200 [5] => 8190 [6] => 8180 [7] => 8174 [8] => 8172 [9] => 8168 [10] => 8162 [11] => 8150 [12] => 8144 [13] => 8138 [14] => 8132 [15] => 8134 [16] => 8130 [17] => 8126 [18] => 8128 [19] => 8124 )

1 Answer 1

7

use

get_the_ID()

inseted of

the_ID() ;

try this

$temp = array();
while ( $wp_query->have_posts() ) : $wp_query->the_post();
    $temp[] = get_the_ID() ;
endwhile;

print_r($temp);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.