最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WP Query Compare cutting off last 2 dates

programmeradmin1浏览0评论

I have a query using 'compare' -- trying to select where meta values are between certain dates:

   $argsTwo = array(
     'post_type' => 'event',
       'meta_key' => '_event_information_year_select',
     'orderby' => 'meta_value',
    'order' => 'ASC',
     'meta_query' => array(
         array(

         'relation' => 'AND',
               array(
                    'key'     => '_event_information_year_select',
                     'value'   => 1999,
                      'compare' => '>'
                ),
                array(
                'key'     => '_event_information_year_select',
                'value'   => 2003,
                 'compare' => '<='

                )

)
           )
      );

My issue is this query only gives me results until 2002. I can't seem to figure out what is going wrong. The values definitely exist in the DB but these $args don't seem to work.

Any ideas?

Thanks

I have a query using 'compare' -- trying to select where meta values are between certain dates:

   $argsTwo = array(
     'post_type' => 'event',
       'meta_key' => '_event_information_year_select',
     'orderby' => 'meta_value',
    'order' => 'ASC',
     'meta_query' => array(
         array(

         'relation' => 'AND',
               array(
                    'key'     => '_event_information_year_select',
                     'value'   => 1999,
                      'compare' => '>'
                ),
                array(
                'key'     => '_event_information_year_select',
                'value'   => 2003,
                 'compare' => '<='

                )

)
           )
      );

My issue is this query only gives me results until 2002. I can't seem to figure out what is going wrong. The values definitely exist in the DB but these $args don't seem to work.

Any ideas?

Thanks

Share Improve this question asked Jul 14, 2019 at 18:15 Best Dev TutorialsBest Dev Tutorials 4451 gold badge7 silver badges21 bronze badges 2
  • 1 Have you considered storing filterable/groupable data as filterable/groupable data using taxonomies? E.g. If these 2 post meta keys were instead part of an event_information_year taxonomy with terms for each year, then you could make a tax_query that's potentially 100x faster, easier to write, and provides a super easy to way to list the years in a UI on the frontend, as well as providing you with dedicated archive templates, and REST API endpoints? – Tom J Nowell Commented Jul 14, 2019 at 18:56
  • e.g. ask for anything that is in the terms 1999, 2000, 2001, 2002, and 2003 – Tom J Nowell Commented Jul 14, 2019 at 19:28
Add a comment  | 

1 Answer 1

Reset to default 1

It looks like your meta_query is nested within an extra array. Aside from that, the code looks valid.

Hope this helps:

$argsTwo = array(
  'post_type' => 'event',
  'meta_key' => '_event_information_year_select',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key'     => '_event_information_year_select',
      'value'   => 1999,
      'compare' => '>'
    ),
    array(
      'key'     => '_event_information_year_select',
      'value'   => 2003,
      'compare' => '<='
    )
  )
);
发布评论

评论列表(0)

  1. 暂无评论