2

I have spent hours on this - hoping someone who knows a bit of PHP can solve it painlessly.

I have a Wordpress query loop which needs to be concatenated together. It kind of works, but I have requested 6 posts, and all 6 are the same!

Here's the code snippet which returns the duplicate posts:

////* New tab *////// $args = array( 'numberposts' => 6, 'orderby' => 'post_date' ); $postslist = get_posts( $args ); $content .= '<div class="tab" id="new"><ul>'; foreach ($postslist as $post) : setup_postdata($post); $content .= '<li><a href="' . get_permalink() . '"</a>' . get_the_title() . '</a></li>'; endforeach; $content .= '</ul></div>'; return $content; // prints all the contents stringed together 

This code produces this list:

http://i.imgur.com/xNyvE0w.png

Any help appreciated...at all! Thanks.

4
  • So what is your question? Why is it giving you 6 things? First off, are there actually 6 things expected to be returned, or are you saying you are expecting a duplicate to only show once? My guess is that since you asked get_posts() for 6 things, it gave you 6 things. It looks like it is up to you to check for duplicates. Commented Apr 3, 2014 at 0:53
  • Do you do have duplicates, or do you have six or more different posts? Commented Apr 3, 2014 at 2:18
  • Yeah the loop is producing the most recent post, but 6 times. Commented Apr 3, 2014 at 9:10
  • Yeah I'm trying to get the recent posts, not the same one multiple times. Commented Apr 3, 2014 at 9:10

1 Answer 1

1

It looks like your code example is taken from inside a function.

a) Please try this:

// ... cut ... global $post; $args = array( 'posts_per_page' => 6, 'orderby' => 'post_date' ); $postslist = get_posts( $args ); $content .= '<div class="tab" id="new"><ul>'; foreach ( $postslist as $post ) : setup_postdata( $post ); $content .= sprintf( '<li><a href="%s">%s</a></li>', get_permalink(), get_the_title() ); endforeach; $content .= '</ul></div>'; wp_reset_postdata(); return $content; // prints all the contents stringed together 

where I added the global $post declaration so setup_postdata( $post ) can modify the global $post object.

b) Or try this:

// ... cut ... $args = array( 'posts_per_page' => 6, 'orderby' => 'post_date' ); $postslist = get_posts( $args ); $content .= '<div class="tab" id="new"><ul>'; foreach ( $postslist as $post ) : $content .= sprintf( '<li><a href="%s">%s</a></li>', get_permalink( $post->ID ), get_the_title( $post->ID ) ); endforeach; $content .= '</ul></div>'; return $content; // prints all the contents stringed together 

where I used the post id as an input argument for get_permalink() and get_the_title().

c) You can also use the raw post title: $post->post_title or if you want to take it through the the_title filter with:

echo apply_filters( 'the_title', $post->post_title ); 

So to wrap it up:

Notice that get_the_title() is based on get_post(), and if you don't use an input argument, then it will try to use the global $post object:

if ( empty( $post ) && isset( $GLOBALS['post'] ) ) $post = $GLOBALS['post']; 

Therefore you always get the same global post object title (usually from the main query) if you don't modify the global object within the secondary loop.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Birgire. Yes, the first solution worked. It was all in the loop, and yours spat out the most recent ones. Great stuff. Thanks so much for this! Martin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.