Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to output the last (chronologically) 4 posts having the tag (slug) "pp".

<ul>
<?php
$args = array( 'posts_per_page' => 4, 'taxonomy' => 'tag', 'field' => 'slug', 'terms' => 'pp' );
$tag_posts = get_posts( $args );
foreach ( $tag_posts as $post ) : 
  setup_postdata( $post ); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; 
wp_reset_postdata(); ?>
</ul>

This snippet though output 4 entries of the same post, which is the fourth from last! How come? :)

share|improve this question
    
Try to add a wp_reset_query() before get_posts. –  Alex Dumitru Nov 30 '13 at 13:36
    
Uhm, doesn't work anymore with that :P –  MultiformeIngegno Nov 30 '13 at 13:38
    
Before the foreach loop do var_dump( $tag_posts ); so you can see if they really are the same post. –  Chris_O Nov 30 '13 at 14:07
    
The result is NULL. The permalink is correct though (it's the one that title says). –  MultiformeIngegno Nov 30 '13 at 14:16
add comment

1 Answer

This is because you are not passing the parameters in the correct way, taxonomy param needs to be passed under tax_query. Please replace the $args with the below:--

$args = array(
    'posts_per_page' => 4,
    'tax_query' => array(
        array(
            'taxonomy' => 'tag',
            'field' => 'slug',
            'terms' => 'pp'
        )
    )
);

For more details check the codex

share|improve this answer
    
I tried but with but now output is blank New snippet: pastebin.com/raw.php?i=kQLFeetN –  MultiformeIngegno Nov 30 '13 at 14:53
    
oops, posts_per_page has a ' where it shouldn't. –  MultiformeIngegno Nov 30 '13 at 14:53
    
Uhm, stil no luck –  MultiformeIngegno Nov 30 '13 at 14:55
    
can you tell me what does var_dump($tag_posts) return here. Please try it before the foreach loop and let me know –  Maruti Mohanty Nov 30 '13 at 15:10
    
Try the code now, there was an apostrophe after 4, Rarst has edited it. please try the above code now Thanks @Rarst –  Maruti Mohanty Nov 30 '13 at 15:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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