0

My query arguments are inspired by a solution here. I needed to get posts with custom field expert_intown but I also needed to exclude posts that have the meta_key coming_soon. My arguments works fine, as in it returns the posts as expected.

Now, I need help to get the results in right order, that is I need the posts with the field expert_intown to appear before the posts with the custom-field coming_soon. I have been trying for a while and currently I have this argument, which again fails.

$intown_args = array(
            'post_type' => 'post',
            'cat'  => 2,
            'numberposts' => -1,
            'post_status'  =>  'publish',
            'meta_query' => array(
                                   'relation' => 'OR',
                                array( //posts that contain the meta_key expert_intown 
                                   'key' => 'expert_intown',
                                   'value' => 'yes',
                                   'compare' => 'EXISTS'
                                ),
                                array( //posts that dont contain the meta_key coming_soon
                                   'key' => 'coming_soon',
                                   'compare' => 'NOT EXISTS'
                                )
                            ),
            'meta_key' => 'expert_intown',
            'orderby' => 'meta_value',
            'order' => 'DESC'
            ); 

            $int_pro = new WP_Query( $intown_args );

NOTE : This query works fine with 'orderby' => title or date, just fails to group together the posts containing the field 'expert_intown'.


Also please note that every value of expert_intown is set to yes, perhaps the reason to fail, as I may be trying to order by values which are all equal. But even in that case shouldn't it be grouped together ?, because right now the posts with the field expert_intown are mixed with posts with field coming_soon.


UPDATE FIRST : As a shot in the dark, I tried the posts_orderby filter as suggested in a solution here. Hopefully to get posts by meta_value and then date, but does not seem to work either. Here is now what the code looks like with the filter.

$intown_args = array(
            'post_type' => 'post',
            'cat'  => 2,
            'numberposts' => -1,
            'post_status'  =>  'publish',
            'meta_query' => array(
                                'relation' => 'OR',
                                array(  
                                   'key' => 'expert_intown',
                                   'value' => 'yes',
                                   'compare' => 'EXISTS'
                                ),
                                array( 
                                   'key' => 'coming_soon',
                                   'compare' => 'NOT EXISTS'
                                )
                            )
            ); 

add_filter( 'posts_orderby', 'modify_orderby' );
function modify_orderby( $orderby ) {
        global $wpdb;
        $orderby = " $wpdb->postmeta.meta_value DESC, $wpdb->posts.post_date DESC ";
        return $orderby;
}
$int_pro = new WP_Query( $intown_args );
remove_filter( 'posts_orderby', 'modify_orderby' );
  • Just a thought...if the 'expert_intown' part of the OR is true, it may or may not have something in 'coming_soon' as well, that's probably why the results are mixed. Just because you sort on the 'expert_intown' value, that won't cause the rows with a 'coming_soon' value to be moved to the bottom. The rows that have a 'yes' for 'expert_intown' will display whatever value is present in that row for 'coming_soon'. Maybe I'm reading your question wrong but perhaps you should use AND instead of OR and possibly use two separate loops. – leromt Jun 2 '14 at 19:00
  • using the AND WILL only return posts with the field expert_intown because the args will be 'expert_intown' exists and 'coming_soon' doesn't. The args are returning exactly what they should. For the second loop try returning 'expert_intown' AND 'coming_soon' exist and a possible needed third loop would be 'coming_soon' exists AND 'expert_intown' does NOT. – leromt Jun 2 '14 at 19:54
  • Any luck, @gurung? – leromt Jun 3 '14 at 2:22
  • @leromt With relation - AND, and both 'expert_intown' and 'coming_soon' set to EXIST - returns no post at all. With same settings but the relation - OR, returns posts only that contains both those meta fields, avoiding the ones that are empty. – gurung Jun 3 '14 at 5:36
  • @leromt With relation - AND, wherein 'coming_soon - EXISTS' and 'expert_intown - NOT EXISTS, returns nothing. – gurung Jun 3 '14 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.