Support

Account

Home Forums General Issues How to sort by ACF field

Solved

How to sort by ACF field

  • I’ve looked at the docs and I see how to sort based on an ACF field, but here’s my dilemma:

    I’m pulling custom post types based on their state, and showing them on a page. Works great. But then I need to sort them by city, and I’m lost at that point. Here’s my current code that works for just pulling them in by state:

    <?php 
     
    	    // args
    	    $args = array(
    		    'numberposts' => -1,
    		    'post_type' => 'OGs',
    		    'meta_key' => 'state',
    		    'meta_value' => 'Texas'
    	    );
    	     
    	    // get results
    	    $the_query = new WP_Query( $args );

    Any help is greatly appreciate. I need it spelled out…I’m a front end guy who knows just a little bit about PHP.

  • Hi,
    in your $args you can’t add multiple ‘meta_key’ => ‘…’, ‘meta_value’=>’…’ on the same level.

    you will want to replace your meta_key and meta_value entry by a multiple array:

    
    $args = array(
    'some_args' => 'some value,
    'meta_query' => array( 
                       array( 
                         'key' => 'state', 
                         'value' => 'Texas', 
                       ),
                       array(
                          'Key' => 'city',
                          'value' => 'Houston'
                       ),
    'some_other_args' => 'some_value'
    );
    

    This should get you the state of Texas and the city of Houston with all the other args you have defined.

    you may want to use variable on your states and city value.

    I haven’t tested this code so let me know how it goes.

  • Thanks l.pirondini,

    I replaced my code with your code above, but it made the page not load. So I took out the lines that say “some_other_args” but it still didn’t work.

    I’m just trying to grab everyone in Texas, and then sort them alphabetically by city. My ACF city field is just called “city.”

  • Any other thoughts on this? Not sure how to make this work.

  • Hi,

    so what you want is get posts by state name and then when outputting them sort them by city.

    maybe you could try that, instead of having 2

    set up your $args like that:

    $args = array(
             'numberposts' => -1,
    	 'post_type' => 'OGs',
             'orderby'   => 'meta_value', 
             'order' => 'DESC',
    	 'meta_key' => 'city',
    );

    this code above should return all the OGs posts ordered by city however the states part is missing i don’t know if you can add it as below.

    $args = array(
             'numberposts' => -1,
    	 'post_type' => 'OGs',
             'orderby'   => 'meta_value', 
             'order' => 'DESC',
    	 'meta_key' => 'city',
             'meta_query' => array( 
                             array( 
                             'key' => 'state', 
                             'value' => 'Texas', 
                             ),
    );

    I diden’t test that but looks like the ‘orderby’ => ‘meta_value’ is a good start.

    let me know how it goes

  • l.pirondini,

    Thanks for your input. I tried it out, but the above (second) section of code caused the page not to load, and I’m not enough of a PHP guy to figure out why.

  • Hi,
    A closing parenthesis was missing in my example

    $args = array(
        'numberposts' => -1,
        'post_type' => 'OGs',
        'orderby'   => 'meta_value', 
        'order' => 'ASC',
        'meta_key' => 'city',
        'meta_query' => array( 
          array( 
            'key' => 'state', 
            'value' => 'texas', 
             ),
         )
    );

    I tested my code in a fresh install of WP, it outputs all the posts with a state value of texas (text input custom field named state) and sort by city alphabetical (with another text input custom field named city).

    the only difference in my test is that i didn’t test with the same post_type, let me know if you still having issues.

  • NICE. You did it!!!!

    Thank you, thank you. That worked beautifully.

    – kyler boudreau

  • I have a similar issue. I have this:

        $args = array(
           'meta_key' => 'event_date',
           'orderby' => 'meta_value_num',
           'order' => 'ASC',
           'cat' => '2'
        );

    and I want to add the additional sorting where the “event_date” custom field is either on or later than the current date. Any help would be appreciated.

  • Hi,
    you can do it like this:

    $currentDate = date('Ymd');	
    $args = array(
        'numberposts' => -1,
        'meta_key' => 'date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
    	'meta_query' => array( 
             array( 
    	        'key'		=> 'date',
    	        'compare'	=> '>=',
    	        'value'		=> $currentDate,
                ),
    	 )
    	);

    this would return all the posts where the “date” field is equal to the current date or later. (you would need to change the “date” on my code to your “event_date” field name and add other arguments you may want to have like “cat”=> ‘2’ the save format of your date field should be set to yymmdd

  • And how might one modify the OP code to output ALL states separately, with associated Cities under each state like:

    ALABAMA
    -city 1
    -city 2
    -city 3

    TENNESSEE
    -city 1
    -city 2
    -city 3

    VIRGINIA
    -city 1
    -city 2
    -city 3

  • Hi,
    I have some similar issue
    In Woocommerce product I have added one ACF group named product_shipping_and_installment and inside this group one field named product_type in this field user will add “AC” Or “CO”.

    Below is the code:

    
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'post_status' => "publish",
        'product_cat'    => $cat->name,
        'meta_query'	=> array(
            'relation'		=> 'AND',
            array(
                'key'		=> 'product_shipping_and_installment_product_type',
                'value'		=> array('AC', 'CO'),
                'compare'	=> 'IN'
            ),
        ),
            
    );
    $loop = new WP_Query($args);
    

    Above screenshot code will provide me all the list of product based on field named product_type but not sorting wise. I would like to show “AC” value product list first and then need to show “CO” product list currently is showing me “CO” product list first and then “AC” product list.
    Any help is greatly appreciate.
    Thanks 🙂

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

The topic ‘How to sort by ACF field’ is closed to new replies.