• Resolved TRDD

    (@traceyrickard)


    Hello,

    I would like to order the filter results by a custom field. Please could you let me know if I could do that like this:

    add_action( 'pre_get_posts', 'sort_rentals' );
    function sort_rentals( $query )
    {
       if if(!is_admin() && is_post_type_archive('rental')) {
                $query->set('meta_key', 'ranking' );
                $query->set('orderby', ''meta_value_num');
            }
        return $query;
    }

    If so, would I need to specify an archive type for each taxonomy?

    Thank you for your help ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • There’s a doubled “if”, but otherwise this should work.

    You can use “is_archive()” to sort the posts for any archive and not just for the “rental” post type archive.
    In fact, you can use any of the conditional tags.

    Like so:

    
    // Changing the order of the main query
    add_action( 'pre_get_posts', 'sort_by_meta_value' );
    
    function sort_by_meta_value( $query )
    {
        if (is_admin()){
            return;
        }
    
       if ( is_archive() ){
    		$query->set('meta_key', 'ranking' );
    		$query->set('orderby', ''meta_value_num');
            }
        return $query;
    }
    Thread Starter TRDD

    (@traceyrickard)

    Thank you very much for your reply, very much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ordering of Results’ is closed to new replies.