admin管理员组文章数量:1122846
I've got custom taxonomy archive pages with a drop down list that allows users to re-order/sort the posts. It basically waits for the user to select from the drop down then uses array_merge( $wp_query->query.....
to create a new query and re-sort the posts. This works fine. I also use wp-pagination to display numbered pagination. This also works fine.
The problem is, when a user re-sorts the list, and then clicks on the pagination to go to page 2 the new sort is lost and reverts back to the original. I can't work this out. Any ideas?
I've got custom taxonomy archive pages with a drop down list that allows users to re-order/sort the posts. It basically waits for the user to select from the drop down then uses array_merge( $wp_query->query.....
to create a new query and re-sort the posts. This works fine. I also use wp-pagination to display numbered pagination. This also works fine.
The problem is, when a user re-sorts the list, and then clicks on the pagination to go to page 2 the new sort is lost and reverts back to the original. I can't work this out. Any ideas?
Share Improve this question asked Jun 24, 2014 at 8:42 Luke SeallLuke Seall 3303 silver badges17 bronze badges1 Answer
Reset to default 0I had the same problem.
I was using a form to sort the data, and a pagination using paginate_links. My solution was to use $_SESSIONS
to store the $_POST
data from the sort (instead of just parsing the $_POST
data), and set the form action to get_permalink()
. Make sure to unset your session if you are not using pagination or if you are not $_POST
ing the sort form.
<script>
function submitform(val) {
document.getElementById('sort-option').value = val;
document.getElementById('form-sort').submit();
}
</script>
<form id="form-sort" action="<?php echo get_permalink(); ?>" method="post">
<ul>
<li>Sort By:</li>
<li><a href="#!" onclick="javascript: submitform('newest');">Newest rating</a>
</li>
<li><a href="#!" onclick="javascript: submitform('highest');">Highest rating</a>
</li>
<li><a href="#!" onclick="javascript: submitform('lowest');">Lowest rating</a>
</li>
</ul>
<input type='hidden' id='sort-option' name='sort-option' value=''>
</form>
-
if($wp_query->get('page') == 0 && empty($_POST) && !empty($_SESSION)) session_unset();
if(!empty($_POST) || !empty($_SESSION)) {
switch($_POST['sort-option']) {
case 'newest':
$_SESSION["sort"] = SORT_DESC;
$_SESSION["sort_by"] = 'review_date';
break;
case 'highest':
$_SESSION["sort"] = SORT_DESC;
$_SESSION["sort_by"] = 'rating';
break;
case 'lowest':
$_SESSION["sort"] = SORT_ASC;
$_SESSION["sort_by"] = 'rating';
break;
}
foreach($reviews as $k => $v) {
$column_id[$k] = $v[$_SESSION["sort_by"]];
}
array_multisort($column_id, $_SESSION["sort"], SORT_NUMERIC, $reviews);
}
If you show some code, I might be able to help you more.
本文标签: Sorting Custom Posts on Archive page with pagination
版权声明:本文标题:Sorting Custom Posts on Archive page with pagination 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283761a1927071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论