Order query by meta_value with multiple custom fields

I would like to get ordered all my posts with multiple cutom fields (WordPress 3).

Exemple, I’ve 2 custom couple meta_key/meta_value :
” order_submenuexpositions / numeric ”
” display_submenuexpositions / boolean “

Read More

I tried like this, but the result it’s not orderer by meta_value from “order_submenuexpositions” meta_key :

SELECT * FROM wp_posts
LEFT JOIN wp_postmeta wpostmetaOrder ON ( wp_posts.ID = wpostmetaOrder.post_id AND wpostmetaOrder.meta_key = 'order_submenuexpositions' )
LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
LEFT JOIN wp_term_relationships wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE wp_postmeta.meta_key = 'display_submenuexpositions'
AND wp_postmeta.meta_value = 'true'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_taxonomy.taxonomy = 'category'
ORDER BY wpostmetaOrder.meta_value ASC,wp_postmeta.meta_value ASC

how can i do it ?

thank you !

Related posts

Leave a Reply

1 comment

  1. You can use meta_query to get only posts with display_submenuexpositions = true and order them by order_submenuexpositions.

    $args = array(
        'orderby' => 'meta_value',
        'meta_key' => 'order_submenuexpositions',
        'meta_query' => array(
            array(
                'key' => 'display_submenuexpositions',
                'value' => 'true',
                'compare' => 'LIKE'
            )
        )
    );
    $query_posts( $args );