WordPress.org

Ready to get started?Download WordPress

Forums

Filter by two meta_keys and orderby another meta_key (2 posts)

  1. CristianOspina
    Member
    Posted 1 year ago #

    This is my code:

    $args = array(
    'post_type' => 'temas',
     'posts_per_page' => '15',
     'meta_key' => 'personas',
     'order' => 'asc',
     'orderby' => 'meta_value',
     'meta_query' => array(
    	'relation' => 'OR',
           array(
               'key' => 'idioma',
               'value' => $idio,
    		   'compare' => '=='
           ),
    	   array(
               'key' => 'idioma_2',
               'value' => $idio,
    		   'compare' => '=='
           )
       ),
    'post_status' => 'publish',
    );

    I want to get all post filtered by meta_keys idioma and idioma_2, and ordered by another meta_key called "personas", but it does not work, if I remove the relation on meta_query all works, but i need the relation OR... I dont know what to do...

  2. CristianOspina
    Member
    Posted 1 year ago #

    This is a part of the SQL:
    AND (wp_postmeta.meta_key = 'personas' OR (mt1.meta_key = 'idioma' AND CAST(mt1.meta_value AS CHAR) = '') OR (mt2.meta_key = 'idioma_2' AND CAST(mt2.meta_value AS CHAR) = '') )
    apparently what happens is that the order meta_key is added to meta_query then the order does not affected, being ordained by default:
    GROUP BY wp_posts.ID ORDER BY wp_posts.post_date desc LIMIT 0, 15

    Then, there is another way to sort by meta_key without combining with meta_query?

    This is the same problem on StackOverflow. But I can not get my code works... Help please!

Topic Closed

This topic has been closed to new replies.

About this Topic