actions - cannot filter after using pre_get_posts

I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed. The

I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed.

The action I have is showing only posts for today's date. This is working fine but when I try to use the default filter for different dates I get no data even though I know there is data. (removing the action shows all the data).

Is there a way I can use pre_get_posts but still filter/show previous dates?

add_action ( 'pre_get_posts',  'modify_query_get_apps_by_date' );

function modify_query_get_apps_by_date( $query ) {

    if ( is_admin() && $query->is_main_query() && $query->get( 'post_type' ) == 'apps' ) {
          $query->set( 'meta_query', [
              [
                  'key' => 'app_date',
                  'value' => date('d-m-Y'),
              ],
          ]);
    }
}

I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed.

The action I have is showing only posts for today's date. This is working fine but when I try to use the default filter for different dates I get no data even though I know there is data. (removing the action shows all the data).

Is there a way I can use pre_get_posts but still filter/show previous dates?

add_action ( 'pre_get_posts',  'modify_query_get_apps_by_date' );

function modify_query_get_apps_by_date( $query ) {

    if ( is_admin() && $query->is_main_query() && $query->get( 'post_type' ) == 'apps' ) {
          $query->set( 'meta_query', [
              [
                  'key' => 'app_date',
                  'value' => date('d-m-Y'),
              ],
          ]);
    }
}
Share Improve this question edited Feb 27, 2020 at 12:24 user10980228 asked Feb 27, 2020 at 10:39 user10980228user10980228 1691 silver badge14 bronze badges 6
  • 1 What is $query_vars ? Did you mean $query->get( 'post_type' ) ? – Sally CJ Commented Feb 27, 2020 at 12:00
  • It was an alternative to s_post_type_archive( 'apps' ) which wasn't working. Sorry, that was meant to be $query->query_vars['post_type'] – user10980228 Commented Feb 27, 2020 at 12:05
  • 1 $query->query_vars['post_type'] = 'apps' uses a single equals sign, are you sure you want to use assignment? Did you mean = and not ==? I would also second Sallys comment that you should use ->get( instead, there's probably another method that's even more appropriate – Tom J Nowell Commented Feb 27, 2020 at 12:10
  • Whether I use single or double the result is the same so it doesn't seem to be that. I am still seeing results for the current day which is what I want but I cannot filter by any other date. That just results in 0 results – user10980228 Commented Feb 27, 2020 at 12:12
  • It should not be a single =, my understanding is that this changes all admin queries to only list posts of that type, it's a bug even if it's not the one you're asking about – Tom J Nowell Commented Feb 27, 2020 at 12:13
 |  Show 1 more comment

1 Answer 1

Reset to default 2

The problem is that you always set the meta query, if you don't want it to apply in a particular situation, you need to check for that situation and account for it. Similar to how you're already checking with is_admin, or is_main_query, etc.

In this situation, you'll want to check the $_GET parameters to detect if the filter has been applied on the posts screen, and only set the meta query if it isn't present. In this case, I believe the parameter used is m, but there are several others in the URL to check against

Further notes:

  • When you get the filter working, you probably won't see the results you're expecting as that filter works on the publish date. If you're expecting to see applications filtered by date, you'll need to detect when a filter is happening and add a different meta query that queries app_date via those filter arguments, then undoes the default filter
  • Filtering posts via their post meta is expensive! Really, really expensive! Treat these as a rare thing you only do occasionally. I've seen major sites brought down by simple post meta queries, it puts a big strain on the database. That's why taxonomy tables were created, otherwise, categories and tags would be stored in post meta. If you can repurpose the publish date, that'll make a huge difference.
  • You're not using a standardised format for your date values. Not only that but they're ambiguous! Use YYY-MM-DD instead and follow the ISO standards agreed upon, and you'll get better compatibility
  • Consider specifying that your values are dates using 'type' => 'DATE'
  • You can make the function easier to read and modify by separating out those conditionals into guards and exiting early, e.g. if ( !is_admin() ){ return; }, this also makes debugging easier, and lets you test each check individually if things go wrong

There is also a bug in your code, that is unrelated to your issue, but still a bug. The code checks if the current query is for a particular post type, but because it uses an assignment operator = instead of a comparison operator ==, instead of checking if they match, it sets/assigns the value. As a result, all queries are now queries for apps. This is unrelated to the issue in your question.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744711171a4589351.html

相关推荐

  • actions - cannot filter after using pre_get_posts

    I am using pre_get_posts() in the backend for a custom post type screen where all the records are usually displayed. The

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信