• Hello,

    I want to order data by double parameters such as meta_value_num and title.

    Code

    $args = array(
    		'posts_per_page' => 500,
    		'orderby' => 'meta_value_num title',
    		'meta_key' => '_ss_views',
    		'order' => 'DESC',
    		'ignore_sticky_posts' => 1
    	);

    In this case all post are ordered by title (DESC) then by meta_value_num (ASC)

    What I want is vice versa – First to order meta_value_num from the highest to lowest (DESC) and then order by title in alphabetical order (ASC).

    Can you suggest some solutions?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can do what you want using filters similar to those shown in this article: http://wordpress.mcdspot.com/2013/01/25/sort-on-presence-of-custom-field-field-value-then-date/

    Thread Starter qsz

    (@qsz)

    You can do what you want using filters similar to those shown in this article: http://wordpress.mcdspot.com/2013/01/25/sort-on-presence-of-custom-field-field-value-then-date/

    Thank you very much, but can you explain what exact code should I write in order to order posts by meta_value_num and title?

    Add this to your functions.php:

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
          return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');

    Put this just before your query:

    $mam_global_orderby = " $wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_title ASC";

    And this just after your query:

    $mam_global_orderby = '';  // Clear the filter
    Thread Starter qsz

    (@qsz)

    Okay I write

    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
          return $orderby;
    }
    add_filter('posts_orderby','mam_posts_orderby');

    in themes functions.php, but I think I done smth wrong in next code.
    Code:

    // Setup and run the query for getting the list of posts to show
    	$args = array(
    		'posts_per_page' => 500,
    		'post_type' => 'product',
    		'orderby' => 'meta_value_num title',
    		'meta_key' => '_ss_views',
    		'order' => 'DESC',
    		'ignore_sticky_posts' => 1
    	);
    	// Add the filter here to get only those
    	// within the setting of how far back
    	// to check.
    	add_filter( 'posts_where', 'within_set_limit' );
    $mam_global_orderby = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_title ASC";
    
    	$posts_in_range = new WP_Query( $args );
    
    $mam_global_orderby = '';  // Clear the filter

    Can you help me please?

    Try using just ‘orderby’ => ‘meta_value_num’, without the title.

    Thread Starter qsz

    (@qsz)

    Thanks, but it does not work.
    I checked functions.php and find there this code for taxonomies

    function my_post_queries( $query ) {
        // not an admin page and is the main query
        if ( !is_admin() && $query->is_main_query() ) {
            if ( is_category() || is_tax() ) {
                $query->set( 'orderby', 'meta_value_num' );
                $query->set( 'meta_key', '_ss_views' );
            }
        }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Maybe adding some changes here may help?
    P.s. i need order by meta_value_num (desc) and by title (asc) in categories.

    Try commenting out the add_action line.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Double Ordering (meta_value_num and title)’ is closed to new replies.