Support

Account

Home Forums General Issues How can the following wp query be sorted by a custom field?

Solving

How can the following wp query be sorted by a custom field?

  • So far the code below is not sorting by the custom field value which contains a number followed by a location: “01.location1”, “02.location2”, …
    Do note I took the code from stackoverflow and modified it, I wasn’t sure whether is in line with the rules to post external links.

    <?php
    $news_cat_ID = get_cat_ID( 'Contact Data' ); 
    $news_cats   = get_categories( "parent=$news_cat_ID" );
    $news_query  = new WP_Query;
    
        foreach ( $news_cats as $news_cat ) :
            $news_query->query( array(
                'cat'                 => $news_cat->term_id,
                'posts_per_page'      => 1,
                'no_found_rows'       => true,
                'ignore_sticky_posts' => true,
                'meta_key'   => 'region_contacto',
                'orderby'    => 'meta_value',
                'order'      => 'ASC',
    
            ));
    ?>
    <?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>
    //Code in the loop
    <?php endwhile ?>
      <?php endforeach ?>
  • The query you’re using should be ordering the posts correctly depending on what type of field it is.

    What kind of field is region_contacto? and what settings does it have?

  • I just noticed an error in the code that you posted.

    try this:

    
    <?php
    $news_cat_ID = get_cat_ID( 'Contact Data' ); 
    $news_cats   = get_categories( "parent=$news_cat_ID" );
    
        foreach ( $news_cats as $news_cat ) :
            $news_query = new WP_Query( array(
                'cat'                 => $news_cat->term_id,
                'posts_per_page'      => 1,
                'no_found_rows'       => true,
                'ignore_sticky_posts' => true,
                'meta_key'   => 'region_contacto',
                'orderby'    => 'meta_value',
                'order'      => 'ASC',
    
            ));
    ?>
    <?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>
    //Code in the loop
    <?php endwhile ?>
      <?php endforeach ?>
    
  • Thank you for the answer! I’m afraid it is still not sorting correctly.. I’m getting elements in this order: 02.La Serena, 00. Santiago, 08.Valdivia, …

    Edit: Changed the value of the select field (Field Type) to only numbers like “00”, “01”, “02”, … and used meta_value_num instead of meta_value
    is not working still.

    Edit2: another option perhaps is to sort after the query? or maybe with a function?

  • Another try based on
    https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

    <?php
    $news_cat_ID = get_cat_ID( 'Datos de Contacto' ); 
    $news_cats   = get_categories( "parent=$news_cat_ID" );
    
        foreach ( $news_cats as $news_cat ) :
            $news_query = new WP_Query( array(
                'cat'                 => $news_cat->term_id,
                'posts_per_page'      => 1,
                'no_found_rows'       => true,
                'ignore_sticky_posts' => true,
                'meta_key'   => 'region_contacto',
                'orderby' => array( 'meta_value_num' => 'ASC', 'modified' => 'DESC' )
    
            ));
    ?>
    <?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>

    Not sure what to try next..

  • do you have other plugin that tries to modify the wp query?

    to debug further,

    try printing out the $news_query->request just after you initialize the WP_Query for the actual MYSQL statement and see if the order is actually format properly.

  • I’m sorry for the noobish question, how do I print it out?
    Is it in the debug mode?

    do you have other plugin that tries to modify the wp query?

    I think I don’t.

  • So, before you do your while loop. add another line right before it:

    <?php var_dump($news_query->request); ?>

    This should let you see what’s the actual sql query that gets executed. We can then debug further from there.

  • Okey it returns the following text:

    Contacto

    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (27) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Copiapó
    [email protected]
    [email protected]
    +56 989025280
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (29) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Curicó
    CentroBudistaOtzerLingCuric%C%B
    [email protected]
    Subteniente Luis Cruz Martínez 812, Daya Center
    +56 982494947
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (25) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    La Serena
    BudismoOtzerLingSerena
    [email protected]
    Puyehue 611, Alto Peñuelas
    +56 994038436
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (24) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Santiago
    CentroBudistaOtzerLing
    [email protected]
    Pasaje Traiguen 2420, 7500000 Providencia
    +56 9997890
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (22) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Valdivia
    BudismoOtzerLingValdivia
    [email protected]
    BellaMente Valdivia, Carlos Anwandter 348
    +56 977802320
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (28) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Vicuña
    OtzerlingVicuna
    [email protected]
    +56 965804128
    string(473) "SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (23) ) AND ( wp_postmeta.meta_key = 'region_contacto' ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_modified DESC LIMIT 0, 1"
    Viña del Mar
    BudismoOtzerLingVRegion
    [email protected]
    3 Poniente 441 local 14-A (entre 5 y 6 Norte), 2520164
    +56 956188137
  • If i understand correctly, you are trying to get all the posts within “Contact Data” category and order those posts by the acf field “region_contacto”.

    The way you run the query will not work, because the sorting will only happens inside each query.

    If you want the posts to be sorted by the acf field, they should be within the same query, like so:

    
    <?php
    $news_cat_ID = get_cat_ID('Contact Data'); 
    
    $news_query = new WP_Query( array(
        'posts_per_page' => -1,
        'cat'            => $news_cat_ID,
        'meta_key'       => 'region_contacto',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
    ));
    
    while ( $news_query->have_posts() ) : $news_query->the_post();
        echo get_field('region_contacto') . '<br/>';
    endwhile; wp_reset_query();
    

    This should give you all the posts inside “Contact Data” category and ordered by the acf “region_contacto” value.

    Cheers

  • Thank you so much!!! Now it is working great!!!!
    There’s only a detail missing: If I have more than one post with the same category. For example two posts with the contact region value as Santiago, the two are displayed instead of only one. Each region has a category assigned to it while “Contact Data”” is the parent category of all of them.

    code I’m using:

    $news_cat_ID = get_cat_ID( 'Contact Data' ); 
        $news_query = new WP_Query( array(
        'posts_per_page' => -1,
        'cat'            => $news_cat_ID,
        'no_found_rows'       => true,
        'ignore_sticky_posts' => true,
        'meta_key'       => 'region_contacto',
        'orderby' => array( 'meta_value' => 'ASC', 'modified' => 'DESC' )
    ));
    ?>
    
    <?php while ( $news_query->have_posts() ) : $news_query->the_post() ?>

    Edit: deleted this line as well:
    $news_cats = get_categories( “parent=$news_cat_ID” );
    And added a bit more of info.

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

The topic ‘How can the following wp query be sorted by a custom field?’ is closed to new replies.