Meta_query key using a Post File Type as key

Support General Meta_query key using a Post File Type as keyResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36337
    Olivier CayrolOlivier Cayrol
    Participant

    Hi,

    I spent ages to figure out how to order my meta_query based on a meta key "Post" File Type.
    I use "Post" File type to link a specific post to a CPT. But the value stored in the DB is the related post ID, and I want to order bye the related post title.

    This is my code :

    $query->set( 'meta_query', 
        array(
            'commune_elu_order' => array(
                'key' => 'commune_elu', // MB Post File Type
                'compare' => 'EXISTS', 
                'type' => 'NUMERIC'
             )
        ) 
    );
            			   
    $query->set( 'orderby', 
        array(
            'commune_elu_order' => 'ASC'
        )
    );

    My result is ordered by "commune_elu_order" ASC as expected.
    But I would like to Orderby the post_title returned by the Key (meta_value) 'commune_elu'.

    The result is ordered by "meta_value" column from table "wp_postmeta"
    Ex of a line I have in my DB:
    meta_id = 41876
    post_id = 1970
    meta_key = commune_elu
    meta_value = 1215

    I would like to order by the post_title of the returned meta_value.
    What will be your recommendation for this situation ?

    #36346
    Long NguyenLong Nguyen
    Moderator

    Hi Olivier,

    WordPress itself does not support ordering by the post_title of the returned meta_value like that. You need to create your own SQL query to join values from more tables.

    #36352
    Olivier CayrolOlivier Cayrol
    Participant

    Thanks, this is what i'm trying to do...

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.