Support » Plugin: Query Wrangler » orderby -> meta_value

  • Resolved jabbadu

    (@jabbadu)


    Hello,
    first – great plugin! I am testing it currently and still am very excited about the functions.
    but unfortunately I could not find a thing. it is possible to sort the posts by a custom field?

    as for example: I have a custom field with “advanced custom fields” created: rating
    Now I have a new “query wrangler” added. As a standard value, the posts are sorted by date, but I want to sort them ASC or DESC with the custom field i’ve added..

    is it possible?

    https://wordpress.org/plugins/query-wrangler/

Viewing 6 replies - 1 through 6 (of 6 total)
  • +1 (subscribed)

    +1 (also looking for sorting through an advanced custom field)

    Thread Starter jabbadu

    (@jabbadu)

    For those who wanna know how to Query Posts and set a orderby->meta_value: http://codecanyon.net/item/quick-ajax-modern-wordpress-ajax-query-posts/5740469

    This premium plugin is my solution. In fact it isnt that powerfull like query wrangler but you can sort by a custom field meta value with a special Shortcode – works great for me.

    Add this to your functions.php

    // Custom sort by rating
    function my_custom_sort( $query ) {
    
        if ( $query->is_archive())
        {
        	$query->set('meta_key', 'rating');
    		$query->set('orderby', 'meta_value_num');
        	$query->set('order', 'ASC');
        }
    	return $query;
    }
    add_action('pre_get_posts', 'my_custom_sort');

    Plugin Author Jonathan Daggerhart

    (@daggerhart)

    Hi All,

    In the most recent update (1.5.31) I’ve added 2 new sort options. Meta Value – for text, and Meta Value Number – for numbers.

    They require you also add a “Meta Key” filter with the appropriate sorting key.

    Using the rating system as an example:

    1. Add a new Meta Key filter, set key to “rating” without quotes
    2. Add a Meta Value Number sort, set to ASC/DESC

    Hope this helps, and thanks Riaan for the help!
    Jonathan

    Nice one Johnathan!

    The code I have posted is a overriding WP function and will force all queries to sort that way, if they have the rating value. Not very flexible.

    This way we have control. Good work. I will definitely be using this functionality.

    Riaan

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘orderby -> meta_value’ is closed to new replies.