Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I think I might be doing something fundamentally wrong in my Wordpress themes since this issue seems to crop up every once in a while and I don't know why.

So basically I created search.php and I use relevanssi to sort my search results by relevancy. When I search for "camera" and do echo get_post_type(); I get "reviews" but if I search for "test" and do echo get_post_type(); I get "articles".

I tried injecting wp_reset_query() just about everywhere to try and see if I was changing it randomly but it's not that. I think WP is confused since my search result page isn't really linked with any specific page so it is not set to a specific post type. Is there a way to have it be a page?

Any help is appreciated

edit:

I added the code below. In the var_dump I just see the query stuff but get_post_type() is different on every search term.

search.php:

<?php
get_header();

global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$args = array(
    'posts_per_page' => get_option('posts_per_page'),
    's' => $search_query['s']
);

echo get_post_type() .'<br>';
var_dump($args);

$search = new WP_Query($args);

if( $search->have_posts() ) {
    while( $search->have_posts() ) {
        $search->the_post();

        include( get_template_directory() . '/includes/templates/search_result.inc.php' );      

    }
    wp_reset_query();
}
?>

<a id="ld-more" href="#search">Load more search results</a>
<?php
get_footer();
share|improve this question
2  
where do you call get_post_type (show us the code), and why are the results you're getting unexpected (what do you expect it to return)? –  Milo Nov 5 '13 at 15:26
    
I think it is more likely that you are confused about how WordPress works :) Post your relevant code please. –  s_ha_dum Nov 5 '13 at 15:29
    
To be more specific: please post your search.php template code. –  Chip Bennett Nov 5 '13 at 15:29
    
Oops sorry guys, search.php content would be extremely handy! I edited the original question with the code. I think this might be a situation where I've misunderstood something in theme development and I've been doing it wrong all this time (hence why I'm trying to solve this once and for all haha) –  Gazillion Nov 5 '13 at 15:31
    
Next question: why are you hijacking the default search query? If the sole purpose is to modify the posts per page, then pre_get_posts is your new best friend. :) –  Chip Bennett Nov 5 '13 at 15:32
show 1 more comment

1 Answer

up vote 2 down vote accepted

Your problem is almost certainly that you've created a second query. That query is going to be out of sync with the main query, hence you are getting unexpected results.

From what I can see, you aren't really doing anything that the main query doesn't already do, so I think you really only need the Loop itself.

get_header();

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

        include( get_template_directory() . '/includes/templates/search_result.inc.php' );      

    }
    wp_reset_query();
}
?>

<a id="ld-more" href="#search">Load more search results</a>
<?php
get_footer();
share|improve this answer
    
Hmm well that solved the problem where I had no results but the post type is still random. Is there a way (and must I) tell wordpress that my search.php is a "page" the same way you set a page to be your blog and your front page? –  Gazillion Nov 5 '13 at 16:02
2  
@Gazillion a search results page isn't a page, it's a search. the post type isn't random, it's the post type of post(s) in your search results. maybe you should back up a few steps and explain why you want it to behave as a page. –  Milo Nov 5 '13 at 16:11
    
Hmm I guess I assumed that it had to be associated with something. In my header code I have a check for a specific sidebar that only appears for a specific post type and since the post type seemed random I would sometimes get the sidebar and sometimes not. Seeing how this is the correct behaviour I just added an is_search check and it works now. I'll just have to be more mindful in the future. I'll mark this answer as correct since it fixed another issue I was having! :) –  Gazillion Nov 5 '13 at 16:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.