Support

Account

Home Forums Front-end Issues Custom Post List filtered by ACF criteria

Solving

Custom Post List filtered by ACF criteria

  • I am trying to get a loop to work for a custom post type but filtered according to certain criteria / custom fields.

    I have custom post type using ACF called “feature”. I want to filter by 3 fields, firstly by a field called “sticky” – this is either a 1 or a 0 (but could be changed if necessary). Then I want to filter by published date – the “sticky” items would also hopefully be filtered by date then the non-sticky items would follow in descending date order.

    I also have a 3rd field called “hidden” which is another 1 or 0 (if it has been switched on (1) then I don’t want to grab that post).

    I have the basic loop code below – but whenever I try to add another field based on some of the code I have found elsewhere nothing seems to change or work.

    The additional criteria that I need is to have pagination.

    What can I add to the argument list below to get it to filter and retrieve the posts in in the order I want?

    Thanks in advance for any help.

    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;	
    $args = array(
    	'paged' => $paged,
    	'post_type'		=> 'feature',
    	'posts_per_page'	=> 5,
    	'orderby' => 'published',                            
    	'order' => 'DESC'
    );
    
    $wp_query = new WP_query();
    $wp_query->query($args);
    if ( $wp_query->have_posts() ) {
    	// loop goes here
    } else {
    	// no posts found
    }
  • You have to have your order by argument as an array:

    $q = new WP_Query( array( 'orderby' => array( 'sticky' => 'DESC', 'published' => 'ASC' , 'hidden' => 'ASC' ) ) );

    Need to be careful how you are calling these keys in though. Remember you need

    'meta_key' => 'start_date', argument in there too.

    I’ve never tried it will muliple custom fields but i’m guessing you will have to have the matching keys as an array also.

  • Hi magicstick

    Thanks for your quick reply.

    I gave your idea a go but I am not sure how to incorporate the meta_key “start_date” into the equation – could you elaborate. Perhaps I don’t need it as I am not using an ACF start date but the original “published” date.

    I have tried to use the following but with no success (I simplified the fields I am using):

    $args = array(
       'paged' => $paged,
       'post_type' => 'feature',
       'orderby' => 'meta_value_num',
       'meta_query' => array(
       'relation' => 'AND',
       array( 'orderby' => array( 'published' => 'DESC' , 'hide_from_archive' !=   '1' ))
       )
    );			
    $wp_query = new WP_query();
    $wp_query->query($args);

    Thanks again for your advice.

  • I believe I have managed to get it to almost work but with just the two fields previous mentioned – I still am not sure how to get the third field (“sticky”) into the argument list – any further help is much appreciated.

    Thanks.

    $args = array(
    	'paged' => $paged,
    	'post_type' => 'feature',
    	'orderby' => 'published',
    	'order' => 'DESC',
    	'posts_per_page' => 5,
    	'meta_query' => array(
    	   array(
    		   'key' => 'hide_from_archive',
    		   'value' => array('0'),
    		   'compare' => 'IN',
    	   )
    	)
    );
  • There was an update in WP 4.2 that will allow you to order posts by multiple meta fields. It’s detailed here: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

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

You must be logged in to reply to this topic.