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
    Commented May 24, 2018 at 0:33
  • currweeknumber is the actual week number $currweeknumber = date("W", strtotime("this week")); represents the actual week as number. Commented May 24, 2018 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
    Commented May 24, 2018 at 16:08

1 Answer 1

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 and acknowledge you have read our privacy policy.

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