1

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();
6
  • 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
    Commented Nov 5, 2013 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
    Commented Nov 5, 2013 at 15:29
  • To be more specific: please post your search.php template code. Commented Nov 5, 2013 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
    Commented Nov 5, 2013 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. :) Commented Nov 5, 2013 at 15:32

1 Answer 1

2

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();
3
  • 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
    Commented Nov 5, 2013 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
    Commented Nov 5, 2013 at 16:11
  • 1
    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
    Commented Nov 5, 2013 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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