[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.
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.
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?
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.
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?