经过更多的测试后,很明显,结果实际上并没有进行自然排序。我做了一些改变。很难看。。。但这是可行的。
Okidokes,所以我无法使用Wordpress的排序功能以我想要的方式对其进行排序,所以我确实采用了usort方法,我认为结果非常好。(PHP警告除外apparently a bug.)
这就是我所做的。(我应该提到我正在使用ACF插件。)
存档中。php,就在我循环查询的地方之前:
<?php if (have_posts()) :
// here we are going to populate the "menu_order"
$statusArr = [\'Gold\',\'Silver\',\'Bronze\']; // set up the desired order in which the posts should arrive
while (have_posts()) : the_post(); // start a loop
$position = array_search(get_field(\'partner_status\'), $statusArr); // find the index number of the post\'s "partner_status" relevant to everything in the array
//$post->menu_order = $position; // update the menu order for the post with that index number (thus building a layered reference)
if ( empty( $post->menu_order ) ) wp_update_post( array(\'ID\'=>$post->ID,\'menu_order\'=>$position) ); // here we physically write a menu order into the post so that on subsequent visits, it will sort correctly
endwhile;
function home_orderby( $a, $b ) { // this function does the comparison for the usort
global $posts;
$apos = $a->menu_order; // compare menu_orders
$bpos = $b->menu_order;
return ( $apos < $bpos ) ? -1 : 1;
}
//@usort( $wp_query->posts, "home_orderby" ); // If all you want is to sort by the order, then you could still use this
rewind_posts(); // rewind posts to use in the main loop that creates the HTML ?>
在函数中。php然后使用pre\\u get\\u posts操作挂钩,如下所示:
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
if( isset($query->query_vars[\'country\']) ) {
$query->set(\'orderby\',array( \'menu_order\' => \'ASC\', \'title\' => \'ASC\' ));
}
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');
现在,我看到我的自定义帖子类型按照我在$statusArray顶部指定的顺序排序。快乐的日子!
彼得,谢谢你的意见。这让我走上了正确的道路。:)