1

I am trying to get a custom field value from all posts listed on current blog paginated page. Not sure if it's clear, let me explain it more.

On blog index page (or on second page of pagination) I have 10 posts. And each post have a value for custom field my-field. So I want to list posts as it is but outside the post list (lets say in footer.php) I want to also list all the values of custom field my-field belonging to current posts.

Not sure exactly how it can be done.

EDIT

To simplify (for understanding), I want to echo posts IDs of all listed posts in footer.php.

2
  • 1
    Create a global variable, load all the custom values there, you could use an array. Then just call that var in the footer and do what you have to do
    – Tomás Cot
    Commented Sep 16, 2014 at 11:53
  • Thanks @TomásCot, yes it will work I think. Just started working in this direction. :)
    – Robert hue
    Commented Sep 16, 2014 at 12:10

2 Answers 2

3

You could just rewind the query to loop through again:

<?php rewind_posts(); ?>

<?php while ( have_posts() ) : the_post(); ?>
    <p>Post Number: <?php the_ID(); ?></p>
<?php endwhile; ?>

THE LOOP

2
  • I do have to say though that Pieter's answer is more versatile. Up-vote to him. Commented Sep 16, 2014 at 18:30
  • Yes, but I ended up using @TomásCot suggestion because it provides lots of flexibility. And don't need to worry about custom queries too. ;)
    – Robert hue
    Commented Sep 16, 2014 at 18:50
2

If I understand you correctly, you just need to get all the ID's. The $wp_query global is available as the main query has already executed, so can just access the $posts property and from there get your post ID's. Something like this will do

global $wp_query;
$posts = $wp_query->posts;
foreach ( $posts as $post ) {
    echo $post->ID;
}

If you need an array of ID's, do the following

global $wp_query;
$posts = $wp_query->posts;

$ids = [];
foreach ( $posts as $post ) {
    $ids[] = $post->ID;
}

?><pre><?php var_dump($ids); ?></pre><?php

All your post ID's will be held in the $ids variable

0

Your Answer

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

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