Ask your WordPress questions! Pay money and get answers fast! (more info)

Custom loop in Genesis causing search result to display all posts WordPress

I am using the following code in a genesis child theme with a custom post type and it's causing the search results to display all posts from that post type, rather than the correct matches.

It is using the search function because it prints the "Search results for: searchterm" but returns the wrong results.


remove_action( 'genesis_loop', 'genesis_do_loop' );

add_action( 'genesis_loop', 'child_do_custom_loop' );

function child_do_custom_loop() {

?>

<div class="td_list_1 td_list_1_deallist">

<?php
$args = array(
'paged' => $paged, // respect pagination
'post_type' => 'travel_alert',
'cat' => get_query_var('cat')

);

$recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();
?>



The above works fine for the main page, category archives, but not search results.

Any suggestions?

Answers (1)

2013-04-05

idt answers:

Try adding this filter:

add_filter('pre_get_posts','SearchFilter');

function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type',array('travel_alert'));
}
return $query;
}


You may need to wrap this with a check if search is done for your custom post type so it does not get triggered on normal search.

$query->set('post_type',array('travel_alert'));


Dan | gteh comments:

Sorry that didn't work.

There is only one post type, and I am not using "posts" anywhere else. The search results are returning all posts from the correct post type but not limiting the posts to the search term.


idt comments:

Have you tried adding rewind_posts()?


function child_do_custom_loop() {
rewind_posts();
?>



<div class="td_list_1 td_list_1_deallist">



<?php

$args = array(

'paged' => $paged, // respect pagination

'post_type' => 'travel_alert',

'cat' => get_query_var('cat')



);



$recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();

?>


Dan | gteh comments:

Didn't work either.

I am confused. I don't see any reason that this should be happening.