3

I would like to be able to query for a specific year and week using WP_Query argument notation. I am using this notation as I would like the page to paginate.

What's the correct "year" and "week" key value notation for performing the query in this manner?

I know I could do something like this, but I would like the page to paginate.

Here's the bit of code I'm using for the key-value wp_query notation:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category_name' => 'whats-happening', 'year' => 2012, 'week' => 46 ,'paged' => $paged);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>

HTML post structure to loop through....

<?php if ($wp_query->max_num_pages > 1) : ?>
    <?php next_posts_link( __('<span class="older">&larr; Older</span>') ); ?>
    <?php previous_posts_link( __('<span class="newer">Newer &rarr;</span>') ); ?>
<?php endif; ?>

<?php
    if (function_exists('wp_pagenavi')){ wp_pagenavi(); }
    $wp_query = null;
    $wp_query = $temp;
    wp_reset_query();
?>
1
  • While your Question is beyond my scope, I thought I'd comment on the first line: $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;. You're unnecessarily running the function get_query_var twice. Better would be: $paged = get_query_var('paged'); if ( ! $paged ) $paged = 1;.
    – akTed
    Jan 20, 2013 at 3:12

1 Answer 1

2

There isn't a week query variable, but there is w: http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

w (int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependent on the "start_of_week" option.

Your args should look like this:

$args = array('category_name' => 'whats-happening', 'year' => 2012, 'w' => 46 ,'paged' => $paged);
5
  • this does in fact work; however, my pagination fails to show up at the bottom of the page with this query. Any idea would this could be?
    – Matt
    Jan 20, 2013 at 15:55
  • You mean the numbered page links on the bottom of the page? Jan 20, 2013 at 21:29
  • The next_posts_link() and previous_posts_link() aren't showing. I'm guessing this is because I am only querying for a specific year and week value? I thought I would be able to to see an "Older" and "Newer" pagination link.
    – Matt
    Jan 21, 2013 at 2:31
  • Are there enough posts for those links to show up? Jan 21, 2013 at 2:50
  • yes, there are enough posts.
    – Matt
    Jan 21, 2013 at 3:30

Your Answer

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

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