WordPress 自定义文章排序

一,根据查询字符串排序

WordPress默认排序是按照文章发布时间的,有时候我们需要按照其他方式来排序,或者提供其他方式排序的功能。如果只是基本的排序,比如按照修改时间,或者按照评论数之类的,不需要做任何改动,直接在url加上orderby参数就可以的。

比如就是

http://test.com/?orderby=comment_count //按评论数量排序
http://test.com/?orderby=modified //按修改时间
http://test.com/?orderby=rand //随机排序
http://test.com/?orderby=ID //ID大小

按照浏览量 http://test.com/?orderby=views

add_action('pre_get_posts’, 'fa_orderby_views’);
function fa_orderby_views($query) {
    if (is_home() && $query->is_main_query() && get_query_var('orderby’) == 'views’) {
        $query->set('meta_key’, 'views’);
        $query->set('orderby’, 'meta_value_num’);
    }
    return $query;
}

 

二,根据自定义字段排序

add_action( 'add_meta_boxes', 'rt_add_custom_box' );
add_action( 'save_post', 'rt_save_postdata' );

function rt_add_custom_box() {
  //需要添加自定义字段的页面
  $screens = array('post'); // 'post', 'page',
  foreach ($screens as $screen) {
    add_meta_box(
      'html_meta_div_id',
      __( '主题自定义字段', 'field_textdomain' ),
      'vm_inner_custom_box',
      $screen
    );
  }
}

// 显示自定义字段编辑框
function rt_inner_custom_box($post){
	// 使用随机数进行核查
	wp_nonce_field(plugin_basename(__FILE__), 'myplugin_noncename');
	// 用于数据输入的实际字段
	// 使用 get_post_meta 从数据库中检索现有的值,并应用到表单中
	$list_order = get_post_meta($post->ID, 'list_order', true);
	echo '<label for="list_order">' . _e("排序字段:", 'field_textdomain') . '</label>';
	echo '<input type="text" id="list_order" name="list_order" value="' . esc_attr($list_order) . '" size="25" />';
}

// 文章保存时,保存自定义数据
function rt_save_postdata($post_id) {
	// 首先,我们需要检查当前用户是否被授权做这个动作。 
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id))
			return;
	} else {
		if (!current_user_can('edit_post', $post_id))
			return;
	}

	// 其次,我们需要检查,是否用户想改变这个值。
	if (!isset($_POST['myplugin_noncename']) || !wp_verify_nonce($_POST['myplugin_noncename'], plugin_basename(__FILE__)))
		return;

	// 第三,我们可以保存值到数据库中
	//如果保存在自定义的表,获取文章ID
	$post_ID = $_POST['post_ID'];
	//过滤用户输入
	$mydata = sanitize_text_field($_POST['list_order']);

	add_post_meta($post_ID, 'list_order', $mydata, true) or
		update_post_meta($post_ID, 'list_order', $mydata);
}

调用方法:

$args = array(
      'orderby'  => 'meta_value',
      'order'   => 'asc',
      'meta_key' => 'list_order' 
    );

这种方法,如果文章之前没有对应的 wp_postmeta 记录,该文章不会被搜索到。

三,根据 wp_posts 表用 menu_order 排序

/**
 * Add page attributes to post
 */
function rt_add_post_attributes()
{
  add_post_type_support('post', 'page-attributes');
}
add_action('init', 'rt_add_post_attributes', 500);

古登堡编辑器以后还需要添加下面两个:

/**
 * Add the menu_order property to the post object being saved
 *
 * @param \WP_Post|\stdClass $post
 * @param WP_REST_Request $request
 *
 * @return \WP_Post
 */
function rt_pre_insert_post($post, \WP_REST_Request $request)
{
  $body = $request->get_body();
  if ($body) {
    $body = json_decode($body);
    if (isset($body->menu_order)) {
      $post->menu_order = $body->menu_order;
    }
  }

  return $post;
}
add_filter('rest_pre_insert_post', 'rt_pre_insert_post', 12, 2);


/**
 * Load the menu_order property for frontend display in the admin
 *
 * @param \WP_REST_Response $response
 * @param \WP_Post $post
 * @param \WP_REST_Request $request
 *
 * @return \WP_REST_Response
 */
function rt_prepare_post(\WP_REST_Response $response, $post, $request)
{
  $response->data['menu_order'] = $post->menu_order;

  return $response;
}
add_filter('rest_prepare_post', 'rt_prepare_post', 12, 3);

调用方法:

$args = array(
      'orderby'  => 'menu_order',
      'order'   => 'desc',
      'posts_per_page' => 8
    );
query_posts($args);
收藏
评论
打赏
WordPress 文章置顶循环
上一篇
WordPress RESTful API 的授权方式
下一篇
像素鱼丸
50126 阅读
91 发布
3 收藏
动态
MirageV 主题 2.16.2 发布
MirageV 主题 v2.6.4 发布
LandV 企业主题 v2.6.0 发布
FishV 主题 v1.14 发布
BotV 插件 v1.7.0 发布
FishV 主题 v1.21.0 发布
MirageV 主题 v2.5 正式发布
MirageV 主题 v2.6.0 发布
生成中...
真诚赞赏,手留余香
登录
注册
重置密码