admin管理员组

文章数量:1024158

I have a posts with a Ymd date custom field. I would like the posts to be able to:

  • Displays the posts where the date is not expired (lower than current date)
  • Displays the posts that have the same Week Number as current day week number

I have achieved the first point by doing a meta_query with the date custom field but I can't find any way to achieve the second point with the same custom field because it's a date rather than a week number.


This is my current code, or how I wanted it to be:

$args['meta_query'] = array(
  'relation' => 'AND',
  array(
    'key' => 'date_held',
    'value' => date('Ymd'),
    'compare' => '>',
  ),
  array(
    'key' => date('W', 'date_held'),
    'value' => date('W'),
    'compare' => '=',
  ),
);

Is there any way I could achieve this without adding another custom field by modifying the current custom field date into week number then compare it within the WP_Query argument?

Edit: Fixed the code to be like what I wanted

I have a posts with a Ymd date custom field. I would like the posts to be able to:

  • Displays the posts where the date is not expired (lower than current date)
  • Displays the posts that have the same Week Number as current day week number

I have achieved the first point by doing a meta_query with the date custom field but I can't find any way to achieve the second point with the same custom field because it's a date rather than a week number.


This is my current code, or how I wanted it to be:

$args['meta_query'] = array(
  'relation' => 'AND',
  array(
    'key' => 'date_held',
    'value' => date('Ymd'),
    'compare' => '>',
  ),
  array(
    'key' => date('W', 'date_held'),
    'value' => date('W'),
    'compare' => '=',
  ),
);

Is there any way I could achieve this without adding another custom field by modifying the current custom field date into week number then compare it within the WP_Query argument?

Edit: Fixed the code to be like what I wanted

本文标签: wp queryWPQuery on custom key and value