Support

Account

Home Forums General Issues Advanced Order Query?

Solved

Advanced Order Query?

  • I’m trying to order a list of posts based on an ACF field number, i/e 1 first 2 second etc.

    There is also a field to determine if the post is show, i/e This post show yes or no?

    I’ve tried both the following.

    	$args = array(
    				
    					'post_type' => array( 'page', 'news-articles', 'about-gorilla','involved' ),
    					'showposts' => 4,
    					'meta_key'		=> 'home_page',
    					'meta_value'	=> 'yes',
    					'orderby' => 'home_order',
    					'order' => 'ASC'
    				);
    // args
    				$args = array(
    					'post_type' => array( 'page', 'news-articles', 'about-gorilla','involved' ),
    					'showposts' => 4,
    					
    					'meta_query'	=> array(
    						'relation'		=> 'AND',
    						array(
    							'meta_key'		=> 'home_page',
    							'meta_value'	=> 'yes'
    						),
    						array(
    							'orderby' => 'home_order',
    							'order' => 'ASC'
    						)
    					)
    				);

    The latter ignores the post type and lists all posts, the former works apart from seems to ignore the ”orderby’ => ‘home_order’,’

    Any ideas?

  • Hi @jez

    Could you please try this code:

    $args = array(
    	'posts_per_page'	=> 4,
        'post_type' => array( 'page', 'news-articles', 'about-gorilla','involved' ),
        'orderby'   => 'meta_value_num',
        'meta_key'  => 'home_order',
        'order'     => 'ASC',
    	'meta_query'	=> array(
    		array(
    			'key'	 	=> 'home_page',
    			'value'	  	=> 'yes',
    			'compare' 	=> '=',
    		),
    	),
    );

    I hope this helps.

  • Brilliant thank you, that works a treat.

    So i just had the meta query in the wrong order?

  • Hi @jez,

    Yes, you had a little bit mistake in your code. To learn more about the query, please take a look at these pages:

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    http://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    Hope this helps.

  • Thank you James.

    Yes i looked at that but couldn’t figure out and got confused by the..

    'orderby' => 'meta_value_num’, and 'compare' => '=‘, sections of the query bits.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.