I have a few different searches on my website:
- "Species profiles" (custom post type search)
- "Glossary" (custom post type search)
- Generic, site-wide search
Currently I'm using search.php
; $_POST["type"]
to determine which search has been used and and $_POST["s"]
for the query term:
<?php
if (isset($_POST["s"])) {
$search_term = $_POST["s"];
}
if (isset($_POST["type"])) {
switch ($_POST["type"]) {
case "profile" :
$post_type = "species";
break;
case "glossary" :
$post_type = "glossary";
break;
default :
$post_type = array( 'post', 'species' );
break;
}
} else {
$post_type = array( 'post', 'species' );
}
$args = array(
's' => $search_term,
'post_type' => $post_type
);
?>
<?php get_template_part('searchresults'); ?>
This works well enough, however when I then try to use next_posts_link
or previous_posts_link
, it doesn't work. The URL changes to mywebsite.com/page/2/
and displays a Sorry, you're looking for something that isn't here
message as per standard 404 on my site.
EDIT I've now updated my code so that it includes $paged
as follows (I've also tried changing get_query_var
to 'paged'
rather than 'page'
but it makes no difference):
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
's' => $search_term,
'post_type' => $post_type,
'paged' => $paged
);
However, the same problem is occurring.
Here is the code from searchresults.php
for @MarkDuncan
<?php global $args; ?>
<?php $query = new WP_Query ( $args ); ?>
<?php if ($query->have_posts()) : ?>
<?php if ($s) : ?>
<p class="info">Search results for <em>‘<?php echo $s ?>’</em></p>
<?php else : ?>
<p class="info">Search results:</p>
<?php endif; ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php
if (get_post_type() == "species") {
get_template_part('list', 'species');
} else {
get_template_part('list', 'index');
}
?>
</div>
<?php endwhile; ?>
<div class="navigation"><?php next_posts_link('« Previous Entries') ?> <?php previous_posts_link('Next Entries »') ?></div>
<?php else : ?>
<?php get_template_part( 'notfound' ); ?>
<?php endif; ?>
Is there a way of resolving this issue?
Thanks in advance,
paged
andpage
for paginated results in the related Qs?$_REQUEST
rather than$_POST