I have this code:


$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;

$termchildren = get_term_children( $term_id, $taxonomy_name );

foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );?>

    class="gdlr-core-event-item-list gdlr-core-style-grid gdlr-core-item-pdlr gdlr-core-column-20  clearfix">

    <div class="gdlr-core-event-item-thumbnail">

            href="<?php echo get_term_link( $term, $taxonomy_name ); ?>">
            <?php $image_corsi = get_field('immagine', $term); ?>
                src="<?php echo $image_corsi['sizes']['corsi-img']; ?>"
                width="700" height="935" alt="" /></a>


    <div class="gdlr-core-event-item-content-wrap">

        <h3 class="gdlr-core-event-item-title"><a
                href="<?php echo get_term_link( $term, $taxonomy_name ); ?>"><?php echo $term->name;?></a>

        <div class="gdlr-core-event-item-info-wrap"><span
                class="gdlr-core-event-item-info gdlr-core-type-time"></span><span
                class="gdlr-core-event-item-info gdlr-core-type-location">
                <div class="clear"></div><a
                    class="gdlr-core-excerpt-read-more gdlr-core-button gdlr-core-rectangle"
                    href="<?php echo get_term_link( $term, $taxonomy_name ); ?>"><?php pll_e('Scopri di più', 'trad-scopri'); ?></a>



The code works and is placed in a custom archive taxonomy child template. But what I would like to do is sort the list of posts by custom field (data) created with the ACF plugin. How should I do this starting from this code? I am inexperienced and I am having a lot of effort.

Thanks in advanced!

1 Answer 1


This example will use the get_posts function to load all the ‘events’ posts ordered by a custom field value of ‘my_acf_field’.

you can using like this, where I added 'my_acf_field' you can place with your ACF field name.


// get posts
$posts = get_posts(array(
    'post_type'         => 'event', // Post Type Name
    'posts_per_page'    => -1,    // '-1' display all post, if you write '2' 
                                          then display only 2 post.
    'meta_key'          => 'my_acf_field',
    'orderby'           => 'meta_value', // get my_acf_field stored value
    'order'             => 'DESC'  // you can set your order type here!

if( $posts ): ?>
    <?php foreach( $posts as $post ): 
        setup_postdata( $post )

         /* write your code here */

            <a href="<?php the_permalink(); ?>"><?php the_title(); ?> (date: <?php the_field('start_date'); ?>)</a>  
             /* 'start_date' is another field name of ACF */
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>

<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.