Reputation:
We have search page, that is using standart global $wp_query and takes parameters like s and category_name from $_GET.
I want to add some parameters in wp_query on search page. But more complicated than parameters passed in $_GET.
That's why I tried to create new WP_Query like
$args = array(
'date_query' => array(
array(
'hour' => 9,
'compare' => '>=',
),
array(
'hour' => 17,
'compare' => '<=',
),
array(
'dayofweek' => array( 2, 6 ),
'compare' => 'BETWEEN',
),
),
'posts_per_page' => -1,
);
$new_query = new WP_Query($args);
And take results in
<?php while ($new_query->have_posts()) : $new_query->the_post(); ?>
But it doesn't work. Even if to reset global $wp_query with
wp_reset_query()
I can rewrite global $wp_query going inside and compare date of every post with my date and to unset post if doesn't match. But I am shure it is not right way to do what I want.
What am I dooing wrong?
Upvotes: 3
Views: 1438
Reputation:
function search_pre_get_posts( $query ) {
if(is_search()){
if(empty($_GET['before'])){$before = '01.01.2050';}else{$before = $_GET['before'];}
if(empty($_GET['after'])){$after = '01.01.2000';}else{$after = $_GET['after'];}
$query->set('date_query', array(
'0' => array(
'after' => $after,
'before' => $before,
'inclusive' => true
)
));
}
}
add_action( 'pre_get_posts', 'search_pre_get_posts', 1 );
Upvotes: 0