apply_filters_ref_array( 'posts_orderby', string $orderby, WP_Query $this )

Filters the ORDER BY clause of the query.

Parameters Parameters


(string) The ORDER BY clause of the query.


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

Top ↑

Source Source

File: wp-includes/class-wp-query.php

View on Trac

Top ↑

Changelog Changelog

Version Description
1.5.1 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    useful to custom rank posts on the front-end,

    add_filter('posts_orderby', 'rank_my_taxonomy_post', 10,2);
    funciton rank_my_taxonomy_post($args, $wp_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 wp_query object to target a specific query, such a a particular category term. 
      $queriedObj = $wp_query->get_queried_object();
      if (isset($queriedObj->taxonomy) && 'category'==$queriedObj->taxonomy && isset($queriedObj->term_id) && $queriedObj->term_id = 10) {
        global $wpdb;
        $args = "{$wpdb->posts}.post_name ASC"; //order by post slug ascending.
      return $args;

You must log in before being able to contribute a note or feedback.