0
$args = array(
'post_type' => 'news',
'posts_per_page' => 5,
'post_status' => 'publish',
'order' => 'ASC',
'tax_query' => array(
    array(
        'taxonomy' => 'week',
        'field' => 'slug',
        'terms' => $currweeknumber,
    )
)
);
$post = get_posts($args);

I have a page which posts 5 news each week. I have a problem. A year passed since I started my blog and this $args query shows me the last year news, not the actual. This is because week number doesn't changed at all, it's the same value from 1 to 52 (basically I have 52 terms with the value of 1,2,3...etc). Is there any way to get the last 5 element of this array, not the first 5? I'm tried with 'order' => ''DESC' but not worked.

3
  • There's a variable here named currweeknumber, but the code that figures that out is missing, can you edit it into your question?
    – Tom J Nowell
    May 24 '18 at 0:33
  • currweeknumber is the actual week number $currweeknumber = date("W", strtotime("this week")); represents the actual week as number. May 24 '18 at 8:17
  • hmmm then you're going to need an additional term, such as a year, or, abandon that and just use date queries and set the posts publish time correctly
    – Tom J Nowell
    May 24 '18 at 16:08
0

add the 'date_query' parameter to your query and include the current year.

$args = array(
  'post_type' => 'news',
  'posts_per_page' => 5,
  'post_status' => 'publish',
  'order' => 'ASC',
  'tax_query' => array(
     array(
      'taxonomy' => 'week',
      'field' => 'slug',
      'terms' => date("W", strtotime("this week")),
    )
  ),
  'date_query' => array(
    array(
      'year'  => date("Y")
    )
  )
);

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.