admin管理员组文章数量:1130349
I have a custom post type which I can set a start and end date in the metadata.
I use the function below to filter the search results and only show results from the post type visitor:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'visitor');
}
return $query;
}
add_filter('pre_get_posts', 'SearchFilter');
I have two metadata fields that contains a start date and an end date:
get_post_meta( get_the_ID(), 'visitor-start-date', true ); // Start date
get_post_meta( get_the_ID(), 'visitor-date', true ); // End date
I want to filter the search results to only show results if the current date (today) is between these two dates.
The dates are in epoch/unix and looks like this: 1539129600 1546214400
How do I include this in my filter I created above to only show results where todays date is between the start and end date?
I have a custom post type which I can set a start and end date in the metadata.
I use the function below to filter the search results and only show results from the post type visitor:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'visitor');
}
return $query;
}
add_filter('pre_get_posts', 'SearchFilter');
I have two metadata fields that contains a start date and an end date:
get_post_meta( get_the_ID(), 'visitor-start-date', true ); // Start date
get_post_meta( get_the_ID(), 'visitor-date', true ); // End date
I want to filter the search results to only show results if the current date (today) is between these two dates.
The dates are in epoch/unix and looks like this: 1539129600 1546214400
How do I include this in my filter I created above to only show results where todays date is between the start and end date?
Share Improve this question asked Nov 1, 2018 at 13:11 joq3joq3 3813 silver badges21 bronze badges1 Answer
Reset to default 1You can use meta queries to filter posts according to your needs.
Here is a sample code:
function SearchFilter( $query ) {
if ( !is_admin() && $query->is_search ) {
$query->set( 'post_type', 'visitor' );
$query->set( 'meta_query', array(
'relation' => 'AND',
array(
'key' => 'visitor-start-date',
'value' => time(),
'compare' => '<='
),
array(
'key' => 'visitor-date',
'value' => time(),
'compare' => '>='
),
) );
}
}
add_action( 'pre_get_posts', 'SearchFilter' ); // pre_get_posts is an action, not a filter, so you don't have to return anything in it
If you want the server time, then use time function as in code above.
On the other hand, if you need to use WP time in comparisons, then use current_time function:
current_time( 'timestamp' );
I have a custom post type which I can set a start and end date in the metadata.
I use the function below to filter the search results and only show results from the post type visitor:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'visitor');
}
return $query;
}
add_filter('pre_get_posts', 'SearchFilter');
I have two metadata fields that contains a start date and an end date:
get_post_meta( get_the_ID(), 'visitor-start-date', true ); // Start date
get_post_meta( get_the_ID(), 'visitor-date', true ); // End date
I want to filter the search results to only show results if the current date (today) is between these two dates.
The dates are in epoch/unix and looks like this: 1539129600 1546214400
How do I include this in my filter I created above to only show results where todays date is between the start and end date?
I have a custom post type which I can set a start and end date in the metadata.
I use the function below to filter the search results and only show results from the post type visitor:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'visitor');
}
return $query;
}
add_filter('pre_get_posts', 'SearchFilter');
I have two metadata fields that contains a start date and an end date:
get_post_meta( get_the_ID(), 'visitor-start-date', true ); // Start date
get_post_meta( get_the_ID(), 'visitor-date', true ); // End date
I want to filter the search results to only show results if the current date (today) is between these two dates.
The dates are in epoch/unix and looks like this: 1539129600 1546214400
How do I include this in my filter I created above to only show results where todays date is between the start and end date?
Share Improve this question asked Nov 1, 2018 at 13:11 joq3joq3 3813 silver badges21 bronze badges1 Answer
Reset to default 1You can use meta queries to filter posts according to your needs.
Here is a sample code:
function SearchFilter( $query ) {
if ( !is_admin() && $query->is_search ) {
$query->set( 'post_type', 'visitor' );
$query->set( 'meta_query', array(
'relation' => 'AND',
array(
'key' => 'visitor-start-date',
'value' => time(),
'compare' => '<='
),
array(
'key' => 'visitor-date',
'value' => time(),
'compare' => '>='
),
) );
}
}
add_action( 'pre_get_posts', 'SearchFilter' ); // pre_get_posts is an action, not a filter, so you don't have to return anything in it
If you want the server time, then use time function as in code above.
On the other hand, if you need to use WP time in comparisons, then use current_time function:
current_time( 'timestamp' );
本文标签: phpOnly show search results with if current date is between two dates
版权声明:本文标题:php - Only show search results with if current date is between two dates? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749214029a2333856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论