Skip Navigation

[Resolved] Filtering only custom post type by date in search view

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer created a search view that displays two types: a custom post type (CPT) and standard WordPress posts. They needed to show only CPT entries published up to a week ago, while standard posts should be displayed regardless of their publication date. The customer struggled with configuring the date filter to apply only to the custom post type.

Solution:

We explained that it is not possible to use different date filters for different post types within the same filter by default. To achieve this, we used the wpv_filter_query hook to apply a custom code snippet that checks the post type and applies a date filter only for the custom post type ("listing"). The filter was set to show records published until 7 days ago while allowing modifications without affecting the filter criteria. The code was also updated to ensure it filtered only by the published date, not the modified date. The customer confirmed that the solution worked as expected.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Mateus Getulio 11 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2719700
1.PNG

Hi,
I've made a search view, where two types are displayed - other plugin custom post type and wp posts.
I need to show only custom post types, that have been published a week ago, while the posts remain shown no mater what date they were published.

So far I have the date filter query, but i can't figure out how to make it to work for the custom post type only.

#2720064

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To suggest the best way to achieve this, I'll need to see how this view is set up in the admin area.

Can you please share temporary admin login details along with a link to a page where this view can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Mateus

#2724899

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I checked it and it looks like it is not possible to use different date filters for different post types in the same filter.

I'm afraid it is necessary to use some custom code to achieve it.

We can use the hook wpv_filter_query to change the behavior of the filter:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Please check a working example where I check if it is either a post or a listing from 1 week ago:

add_filter( 'wpv_filter_query', 'filter_query_by_post_type', 101, 3);
function filter_query_by_post_type( $view_args, $view_settings, $view_id ){

    // The View ID goes here
    if ( 10097 == $view_id ) {

        // Check if the post type is 'listing'
        if ( isset( $view_args['post_type'] ) && in_array( 'listings', (array) $view_args['post_type'] ) ) {
            
            // Apply a date filter to show only records from the last week for 'listing' post type
            $view_args['date_query'] = array(
                array(
                    'after' => '1 week ago',
                    'inclusive' => true,
                ),
            );
        
        } elseif ( isset( $view_args['post_type'] ) && in_array( 'post', (array) $view_args['post_type'] ) ) {
            
            // No date filter for 'post' post type, just bring all posts
            if ( isset( $view_args['date_query'] ) ) {
                unset( $view_args['date_query'] );
            }        
        }
    }

    return $view_args;
}

I added the code to the themes's functions.php file and it seems to be working properly, I made sure to target only the view of ID 10097 'Search-results-clean', can you please check it to make sure it is working as expected?

Thank you,
Mateus

#2725281
4.PNG
2.PNG

Hi, I can't check it out, because the search view is not showing now.
I've attached images of what it looks like now vs how it should be.

*Edit - the whole search bar has been deleted somehow, I'll re-create it again and see if it works.

#2725415

I've re-created the search bar and move the custom code to toolset's custom code section.

I've edited the code to show 'before' => '7 days ago', since I want to show custom posts that were published until 7 days ago and hide custom posts that have been published during the 7 days until this day of the week.
Example: listing has published date of last sunday and today is current sunday - it's shown. If it's current monday - the listing should remain hidden.

I've tested by changing published date of the listing to this week and it does work.

My question is will this affect modified posts too? I need the query to include published date only, so user can make modifications all the time until they are visible, without that changing the "7 days ago" rule.

#2725749

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I must have accidentally affected the search bar, I apologize, thank you for restoring it.

I adjusted the code a little so that it doesn't check the modified date, I tested it, and even after updating a listing, it will still display if it is older than 1 week ago:

add_filter( 'wpv_filter_query', 'filter_query_by_post_type', 101, 3);
function filter_query_by_post_type( $view_args, $view_settings, $view_id ){
     
    if ( 10097 == $view_id ) {
 
        // Check if the post type is 'listing'
        if ( isset( $view_args['post_type'] ) && in_array( 'listings', (array) $view_args['post_type'] ) ) {
             
            // Apply a date filter to show only records published until 7 days ago
            $view_args['date_query'] = array(
                array(
                    'before' => '7 days ago',
                    'inclusive' => true,
                    'column' => 'post_date', // Ensure it filters only by the published date
                ),
            );
         
        } elseif ( isset( $view_args['post_type'] ) && in_array( 'post', (array) $view_args['post_type'] ) ) {
             
            // No date filter for 'post' post type, just bring all posts
            if ( isset( $view_args['date_query'] ) ) {
                unset( $view_args['date_query'] );
            }        
        }
    }
 
    return $view_args;
}

Can you please test it and confirm that it is working as expected now?

Thank you,
Mateus

#2726995

Thank you - it's working!