Hi,
Thanks for looking at my post.
I created a plugin that creates a custom post type. The custom post type has an archive page with pagination. That all works fine. The archive page also has two html select elements that sort the posts, and that also works correctly. The problem is that when I use the pagination links to move to a new page, the posts return to the default sort (by date).
I am doing nothing fancy in the archive file:
<form method="post" id="sortForm">
<label for="sort">Sort by: </label>
<select name="sort" onchange='this.form.submit()'>
<option value="title"<?php if (isset($_POST['sort']) && $_POST['sort'] == "title") echo " selected"; ?>>Title</option>
<option value="author"<?php if (isset($_POST['sort']) && $_POST['sort'] == "author") echo " selected"; ?>>Author</option>
</select>
<select name="dir" onchange='this.form.submit()'>
<option value="asc"<?php if (isset($_POST['dir']) && $_POST['dir'] == "asc") echo " selected"; ?>>Ascending</option>
<option value="desc"<?php if (isset($_POST['dir']) && $_POST['dir'] == "desc") echo " selected"; ?>>Descending</option>
</select>
</form>
<?php if (have_posts ()) { ?>
<?php while (have_posts ()) { (the_post()); ?>
// normal loop display stuff
<?php kriesi_pagination(); ?>
To actually do the sort from those select elements, I hooked into pre_get_posts:
function sort_posts( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive('cchs_library')) {
if (isset($_POST['sort'])) {
$dir = isset($_POST['dir']) && $_POST['dir'] == "asc" ? "asc" : "desc";
$query->set('order', $dir);
if ($_POST['sort'] == 'title') {
$query->set('orderby', 'title');
} else if($_POST['sort'] == 'author' ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'library_author');
}
}
}
}
add_action( 'pre_get_posts', 'sort_posts' );
The pagination function isn't mine, it came with the theme, but here it is:
function kriesi_pagination($pages = '', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<ul class='pagination clearfix'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a class='button-small grey rounded3' href='".get_pagenum_link(1)."'>«</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a class='button-small rounded3 grey' href='".get_pagenum_link($paged - 1)."'>‹</a></li>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<li><span class='button-small-theme rounded3 current'>".$i."</span></li>":"<li><a class='button-small grey rounded3 inactive' href='".get_pagenum_link($i)."' >".$i."</a></li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li><a class='button-small grey rounded3' href='".get_pagenum_link($paged + 1)."'>›</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a class='button-small grey rounded3' href='".get_pagenum_link($pages)."'>»</a></li>";
echo "</ul>\n";
}
}
Once again, my question is why isn't the sorting preserved when I use the paginator to go to a new page? If I print out the query object, orderby is not set after I paginate.
The live code is a little more cluttered, but if you want to see the page it is here: