• Hi, i’m extracting a list of posts using the following “args” for a WP_Query:

    $args = array(
    	'post_type' => array( 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5' ),
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'position',
    			'value' => 'first',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'focus',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		)
    	)
    );
    $query = new WP_Query($args);

    On these five custom post types, i have one common custom field that’s “article_position”, for some post types i have a custom field named “position” and for the others i have a custom field named “focus”.
    I use “position” and “focus” to retrive the list of posts from the group of five custom post types: these that have the value “first” for position or “yes” for focus will be on the queried list.
    Till this point it works.
    Now i have to order this list using another custom field named “article_position” that appears on all the custom post types, it’s common to all.

    So i’ve added a couple of code lines as here:

    $args = array(
    	'post_type' => array( 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5' ),
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'position',
    			'value' => 'first',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'focus',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		)
    	),
    	'meta_key' => 'article_position',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC'
    );
    $query = new WP_Query($args);

    But seems that the last lines don’t add anything so useful to reach my goal.
    I’ve searched on codex and google but without success.
    Any hint would be greatly appreciated, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Maybe the article_position key needs to be in the meta_query.
    [untested]

    $args = array(
    	'post_type' => array( 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5' ),
    	'posts_per_page' => -1,
    	'meta_key' => 'article_position',
    	'orderby' => 'meta_value_num',
    	'order' => 'DESC',
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'position',
    			'value' => 'first',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'focus',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'article_position',
    			'compare' => 'EXISTS',
    		)
    	),
    );

    Thread Starter Rics1983

    (@rics1983)

    Nope, although the modify it still don’t work.
    I’m trying a lot of modifies but haven’t found the solution…
    I’ve tried also to make a var_dump of the WP_Query to see the SQL raw equivalent code, but sincerely it goes a bit far than my SQL knowledge.
    Anyway here it is the code paste of the SQL query after your code advice:

    SELECT wp_posts.*
    FROM wp_posts
    INNER JOIN wp_postmeta
    ON wp_posts.ID = wp_postmeta.post_id
    INNER JOIN wp_postmeta AS mt1
    ON (wp_posts.ID = mt1.post_id)
    INNER JOIN wp_postmeta AS mt2
    ON (wp_posts.ID = mt2.post_id)
    WHERE 1=1
    AND wp_posts.post_type IN ('post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5')
    AND (wp_posts.post_status = 'publish'
            OR wp_posts.post_author = 1
            AND wp_posts.post_status = 'private'
    )
    AND (wp_postmeta.meta_key = 'article_position'
            OR wp_postmeta.meta_key = 'article_position'
            OR (mt1.meta_key = 'position'
                    AND CAST(mt1.meta_value AS CHAR) LIKE '%first%'
            )
            OR (mt2.meta_key = 'focus'
                    AND CAST(mt2.meta_value AS CHAR) LIKE '%yes%'
            )
    )
    GROUP BY wp_posts.ID
    ORDER BY wp_postmeta.meta_value+0
    DESC
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query with meta_query and orderby meta_value’ is closed to new replies.