Support

Account

Home Forums Add-ons Flexible Content Field Getting 5 latest Flexible Sub Fields and display in loop.

Helping

Getting 5 latest Flexible Sub Fields and display in loop.

  • Hello, I am at the finishing stages of my website and Flexible Fields have proven invaluable. Sadly I have ran into a major issue that I cannot even begin to understand.

    For example-
    Field – Sub Field
    1 – 1
    1 – 2
    2 – 1
    1 – 3

    As you can see I jump between flexible fields. Now on the main page for example I need to display the latest 5 flexible fields entries in creation order without grouping them into the main fields.

    For example here is a code im using to get the latest comments from posts.

    <?php $commentslatest_a = array(
       'status'       => 'approve',
       'order'        => 'DESC',
       'number'       => '5',
    );
    
    // The Query
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $commentslatest_a );
    
    // Comment Loop
    if ( $comments ) {
    	foreach ( $comments as $comment ) {
    		echo '<div class="commentHeader"><a class="author" href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">';
    		echo get_avatar( $comment->comment_author_email, $avatar_size );
    		echo the_title();
    		echo get_comment_author( $comment->comment_ID ) . ':</a></div>';
    		echo '<div class="commentContent">' .  wp_trim_words($comment->comment_content, 10, '<a class="author" href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '"> ...See Full Comment</a>') . '</div>';
    	}
    } else {
    	echo 'No comments found.';
    }?>

    *Edit* Simplest way I can explain is I need to display that latest 5 Flexible Sub Fields from multiple Flexible Fields, in creation order (Or using the time stamp date meta I add on creating a new sub field). More importantly ONLY the Flexible Fields as I have tried pulling latest meta data but it then includes a lot of irrelevant meta entries.

    Thank You.

  • Hopefully you eventually found some solution for this. For anyone else that’s looking for something similar, there isn’t really any way to know which layout was created when or most recently. If you need to do something like this then you need to do something along the line of with the OP edit suggest. You need to hook into acf/save_post and set some additional meta values that indicate when a layout or some other value was created. See https://www.advancedcustomfields.com/resources/acfsave_post/ for more information on this hook.

    Basically you can add this to happen before ACF saves values with a priority < 10 and you can compare the submitted values with what already exists. You can use the WP function get_post_meta to get values currently stored in the database and bypass ACF and compare those values to what’s in the $_POST array. Then you can update some other meta_key with the date that anything new is added and what was added, for example the layout row. This could get extremely complicated if someone decides to delete a row and I’m sure that it is far more complicated than this brief outline, but it might be a place to start.

    Hope that helps someone.

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

You must be logged in to reply to this topic.