我最近克服了同样的挑战。我们需要一种方法来为多个类别中的每个帖子保存不同的排序顺序
wp\\U term\\U relationships表中有一个未使用的列,
term_order
默认值为0,已转换为整数。
当一个术语被分配给一篇文章时,它会在该表中为分配的每个术语获取一个条目。object\\u ID(post\\u ID)、term\\u taxonomy\\u ID和term order设置为0。
挑战:
- 术语\\u分类ID有时与术语\\u ID不同,因此您必须了解每个类别的术语\\u分类ID。
由于默认值始终为0,您需要一种方法将其设置为更高的数字,否则您的所有新帖子都会排在第一位。
简单的方法就是将数据库默认值更改为更高的数字。另一种方法是创建函数并将其附加到save\\u post、update\\u post或draft\\u to\\u publish挂钩您还需要一种查询这些帖子的方法,因为term_order
不是WP\\U查询类的一部分。
使用posts\\u where筛选或编写自定义查询函数您还需要一种对这些帖子进行排序的方法。我将概述我使用的Ajax拖放方法,但您也可以使用metabox或自定义字段。
Ajax拖放排序:
您需要创建一个管理选项页面,查询帖子,并将其输出到自定义表或无序列表。您还可以使用Ajax来运行它,这样用户只需从选择框中选择一个类别,然后从该类别加载帖子。(我只提供Ajax和Php函数来保存订单)。
PHP wp\\U ajax函数:
add_action ( \'wp_ajax_item_sort\', \'wnd_save_item_order\' );
function wnd_save_item_order () {
global $wpdb;
$wpdb->flush ();
$posts = $_POST[ \'order\' ];
$str = str_replace ( "post-", "", $posts );
$order = explode ( \',\', $str );
$cat_id = (int)$_POST[ \'cat\' ];
$counter = 1;
foreach ( $order as $item_id ) {
$wpdb->query ( "UPDATE $wpdb->term_relationships SET term_order = \'$counter\' WHERE object_id = \'$item_id\' AND term_taxonomy_id = \'$cat_id\'" );
$counter++;
}
$response = \'<div id="message" class="updated fade"> <p>Sort Order successfully updated</p></div>\';
echo $response;
die( \'<div id="message" class="updated fade"> <p>An error occured, order has not been saved.</p></div>\' );
}
jQuery Ajax:
// Add this to the admin_enque_scripts and do a $pagenow check.
function sort_order_js() { ?>
jQuery(document).ready(function($) {
var catID = $("#cat-select option:selected").val()
$("#" + catID + "-save-order").bind("click", function() {
$("#" + catID + "-load-animation").show();
$.post(ajaxurl, { action:\'item_sort\', cat: catID, pos: position, order: $("#" + catID + "-sortable").sortable(\'toArray\').toString() },
function(response) {
$("#" + catID + "-update-response").text(\'Updated\');
$("#" + "-load-animation").hide();
});
return false;
});
<?php } ?>
我们如何获得新订购的帖子
posts\\U orderby筛选器:
add_filter(\'posts_orderby\', \'custom_posts_orderby\');
function custom_posts_orderby($order) {
$order_by = \'wp_term_relationships.term_order\';
$direction = \'DESC\';
$order = " ORDER BY $order_by $direction";
return $order;
}
可以运行此函数,向其传递一个类似于WP\\u query的参数数组,它将返回一个post\\u id数组
function custom_get_post_ids( $args ) {
$defaults = array (
\'category_id\' => NULL,
\'exclude_array\' => array (),
\'post_status_array\' => array ( \'publish\' ),
\'post_status\' => NULL,
\'post_type_array\' => array ( \'post\' ),
\'post_type\' => NULL,
\'offset\' => 0,
\'length\' => 7,
\'order_by\' => \'wp_term_relationships.term_order\',
\'order_direction\' => \'ASC\',
\'secondary_order_by\' => \'post_date\',
\'secondary_order_direction\' => \'DESC\',
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if ( isset( $post_type ) ) {
$post_type_array = array ( $post_type );
}
// If the post_status passed to us is a string, convert it to an array
if ( isset( $post_status ) ) {
$post_status_array = array ( $post_status );
}
global $wpdb;
$query = \'SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)\';
$query .= \' WHERE wp_term_relationships.term_taxonomy_id IN (\' . intval( $category_id ) . \')\';
if ( count( $exclude_array ) > 0 ) {
$exclude = implode( \',\', $exclude_array );
$query .= \' AND wp_posts.ID NOT IN (\' . $exclude . \')\';
}
$query .= " AND wp_posts.post_type IN(\'" . implode( "\',\'", $post_type_array ) . "\')";
$query .= " AND wp_posts.post_status IN(\'" . implode( "\',\'", $post_status_array ) . "\')";
$query .= " ORDER BY $order_by $order_direction";
if ( ! empty( $secondary_order_by ) ) {
$query .= ",$secondary_order_by $secondary_order_direction";
}
$query .= " LIMIT $offset, $length /*_get_post_ids() */";
$num_results = $wpdb->query( $query );
$res = array ();
foreach ( $wpdb->last_result as $r ) {
array_push( $res, intval( $r->ID ) );
}
return ( $res );
}
最后,我们只需要设置新帖子,使其term\\u taxonomy\\u id高于通常要订购的帖子数量
function default_sort_order() {
global $post, $wpdb;
$categories = get_the_category( $post->ID);
$id = $post->ID;
foreach ( $categories as $cat ) {
$cat_id = $cat->term_taxonomy_id;
$wpdb->query ( "UPDATE $wpdb->term_relationships SET term_order = 20 WHERE object_id = \'$id\' AND term_taxonomy_id = \'$cat_id\'" );
}
}
还在这里这种方法已经过测试,并被一家大型新闻机构用来设置主页和分类页上的显示顺序。