Question

I'm trying to figure out why this won't display a list of all wines in ascending order. From what i understand it seems this was the correct way to do it. It displays all the wines correctly on the page, but it doesn't order them.

            query_posts(array( 'post_type' => 'wineinput_custom', 'meta_key' => 'wine', 'showposts' => -1,'tax_query' => array(
        array(
        'taxonomy' => 'categories',
        'terms' => $subcategory3->term_id,
        'field' => 'term_id',

            )
        ),
        'orderby' => 'meta_value_num',
        'order' => 'ASC' )
        );

Custom Taxonomy: categories. Custom post_type: wineinput_custom Field to order it by: wine.

Any help would be appreciated :)

Était-ce utile?

La solution

Add this function to your function.php file,

function orderby_custom_fields( $orderby )
{
    global $wpdb;
    $orderby = "$wpdb->postmeta.meta_value ASC";
    remove_filter( 'posts_orderby', 'orderby_custom_fields' );
    return $orderby;
}

and now add this query_post data in your file,

$args = array(
    'post_type' => 'wineinput_custom',
    'meta_key' => 'wine',
    'showposts' => -1,
    'tax_query' => array( 
        array(
            'taxonomy' => 'categories',
            'terms' => $subcategory3->term_id,
            'field' => 'term_id'
        )
    ),
);
add_filter( 'posts_orderby', 'orderby_custom_fields' );
query_posts($args);

Hope this will help you...!!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top