Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Your code is correct, but it’s in the wrong place. Even without Relevanssi, doing it like this is not a good idea: you’re making WordPress run the search twice.

    Instead of running a custom query on the search results template, use a filter to adjust the query parameters. Use pre_get_posts filter to insert your parameters to the query before the search is run. If you want the changes to only target the search, do it like this:

    add_filter('pre_get_posts', 'ron_date_function');
    function ron_date_function($query) {
    	if (is_search()) {
    	    $day = 1;
    	    $today = date('Ymd');
    		$timecutoff = $today - $day;
    		$onbekend ='';
    		$meta_query = array(
    			'relation' => 'OR',
    			array(
    				'key' => 'verloopdatum',
    				'value' => $timecutoff ,
    				'compare' => '>'
    			),
    			array(
    				'key' => 'verloopdatum',
    				'value' => $onbekend,
    				'compare' => '='
    			)
    		);
    		$query->set('post_type', 'post');
    		$query->set('meta_query', $meta_query);
    	}
    }

    Something like this. This should work fine with or without Relevanssi. Just remove the references to the custom query from your template, and just use the default $wp_query.

    Thread Starter sk33l0

    (@sk33l0)

    Thnx for the feedback. With your peace of code it only shows te post with value “onbekend” but not the posts from future dates.

    can you tell me what is wrong?

    Thread Starter sk33l0

    (@sk33l0)

    i updated my code here https://pastebin.com/MwsTTene

    Plugin Author Mikko Saari

    (@msaari)

    With or without Relevanssi?

    Anyway, move the filter code to functions.php – that’s the right place for it – and then change this:

    $query = new WP_Query( $args );
    if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post();

    to this

    if ( have_posts() ) { while ( have_posts() ) { the_post();

    You don’t need to create any additional queries on the search page.

    Thread Starter sk33l0

    (@sk33l0)

    With Relevanssi offcourse

    Plugin Author Mikko Saari

    (@msaari)

    Well, with the new WP_Query() line in place, you’re not seeing results from Relevanssi in the first place. Fix that, and then we’ll see how Relevanssi works.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Search results with custom query’ is closed to new replies.