One of my WordPress sites is a monthly newsletter sent out by a small team to a big organisation that employs over 1,000 people. The main site works well, but I'm working on the archive pages just now.

Every month, one article is designated the 'featured post'. It's given pride of place at the top of the homepage. I do this by applying the tag 'featured' to the post. I know there's probably better ways to do this than tags, but I've got 12 non-technical people posting items to this newsletter and this worked for them before custom post types came along.

On index.php, I do this:

query_posts(array('tag' => 'featured'));

What I'd really like, is to be able to do this on the archives. I'm trying to get it so that, on the monthly archive page, the post tagged 'featured' for that month shows up at the top, followed by the rest of the posts (as normal).

Has anyone else made similar changes to the archive pages before? Google doesn't seem too helpful -- searching for 'wordpress customize archive' just throws up a few CSS tutorials.

  • Brief summary: inside an archive page, I'd like a way to say 'get posts with this tag, but only within the archive date'. – Lexx Koto Mar 23 '12 at 13:43
  • You're on your way to victory, just use the year and monthnum arguments when querying. codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters don't forget to exclude it from the main loop. – soulseekah Mar 23 '12 at 14:17
  • @Soulseekah Done. If you post this as an answer rather than a comment, I'll accept it. Thanks. query_posts(array('tag' => 'top-left', 'year' => get_the_date( 'Y' ), 'month' => get_the_date( 'm' ))); – Lexx Koto Mar 23 '12 at 14:40

The query_posts function documentation does give an example on how to filter by time as well. Albeit, small and out of context.

The WP_Query documentation give much more information, including on Time Parameters that it accepts. query_posts is just a wrapper to methods provided by the global instance of the WP_Query class.

So, the correct argument set would be something of this sort:

$args = array(
    'tag' => 'the-tag',
    'year' => get_query_var( 'year' ),
    'month' => get_query_var( 'monthnum' )
query_posts( $args );

get_query_var will usually contain the year and the month for an archive.

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.