posts_orderby

最后更新于:2021-11-27 15:09:27

apply_filters_ref_array( ‘posts_orderby’, string $orderby, GC_Query $query )

Filters the ORDER BY clause of the query.

参数

$orderby

(string)
The ORDER BY clause of the query.

$query

(GC_Query)
The GC_Query instance (passed by reference).

源文件

文件: gc-includes/class-gc-query.php

View on Trac

add_filter('posts_orderby', 'edit_posts_orderby');
add_filter('posts_join_paged','edit_posts_join_paged');

function edit_posts_join_paged($join_paged_statement) {
	$join_paged_statement = "LEFT JOIN gc_gdsr_data_article gdsra ON gdsra.post_id = gc_posts.ID";
	return $join_paged_statement;	
}

function edit_posts_orderby($orderby_statement) {
	$orderby_statement = "(gdsra.user_votes_total_sum/gdsra.user_votes_count) DESC";
	return $orderby_statement;
}

add_filter('posts_orderby', 'rank_my_taxonomy_post', 10,2);
funciton rank_my_taxonomy_post($args, $gc_query){
  //first make sure this is not an admin dashboard query.
  if(is_admin()) return $args;
  //you may also want to check if this is the main query, rather than a secondary query such as widgets or menus.
  if(!is_main_query()) return $args;
  //you can use the gc_query object to target a specific query, such a a particular category term. 
  $queriedObj = $gc_query->get_queried_object();
  if (isset($queriedObj->taxonomy) && 'category'==$queriedObj->taxonomy && isset($queriedObj->term_id) && $queriedObj->term_id = 10) {
    global $gcdb;
    $args = "{$gcdb->posts}.post_name ASC"; //order by post slug ascending.
  }     
  return $args;
} 

// Add the callback to the posts_orderby filter
add_filter('posts_orderby', 'orderby_pages_callback', 10, 2);

// The posts_orderby filter
function orderby_pages_callback($orderby_statement, $gc_query) {
	# Verify correct post type, or any other query variable
	if ($gc_query->get("post_type") === "page") {
		# In this trivial example add a reverse menu order sort
		return "gc_posts.menu_order DESC";
	} else {
		# Use provided statement instead 
		return $orderby_statement;
	}
}

// Example GC_Query that loads all pages.
// The above filter callback will cause these to have a reverse menu order sort
$pages_query = new GC_Query(array(
	"post_type" => "page"
));