Home Forums General Issues Include customized excerpt filter with ACF Post Object field


Include customized excerpt filter with ACF Post Object field

  • I customize the excerpt for one of my custom post types using a filter that replaces the default WordPress excerpt with one of my ACF fields like this:

    function my_theme_custom_excerpt( $excerpt, $post = null ) {
    	$post_id = ( $post ) ? $post->ID : get_the_ID();
    	if ( get_post_type() == 'my_custom_post_type') {
    		// Return ACF description field instead of default excerpt
    		return get_field( 'description_field', $post_id );
    	return $excerpt;
    add_filter( 'the_excerpt', 'my_theme_custom_excerpt' );
    add_filter( 'get_the_excerpt', 'my_theme_custom_excerpt' );

    This works great almost everywhere, but I just noticed that if I have an ACF Post Object field, when I use get_the_excerpt( $the_acf_post_object ) on one of my custom post types, the excerpt field is empty even though it shouldn’t be.

    So it seems like the custom excerpt filters don’t execute when retrieving the ACF Post Object field. Is there a way to force that excerpt filter to run on the Post Object field?

  • How are you looping over the object field and showing the excerpt?

  • @hube2 Thanks for the response. I loop through an ACF Repeater that has a single Clone field of a Post Object:

    <?php while ( have_rows( 'my_posts_repeater' ) ) : the_row(); ?>
        <?php echo get_sub_field( 'my_clone_field_post_object' )->ID; ?>
        <?php echo get_the_excerpt( get_sub_field( 'my_clone_field_post_object' )->ID ); ?>
    <?php endwhile; ?>

    The ID displays correctly in the example above but the excerpt is empty. Is it possible that the problem is related to using a Clone field as the Post Object?

  • The hook for get_the_excerpt passes 3 arguments.

    apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );

    You need a different filter for each of these hooks.

    The first line of your filters will need to be different based on the hook

    $post_id = ( $post ) ? $post->ID : get_the_ID();

    for the “get_the_excerpt” filter you need to look at the 3rd argument $post->ID

    This part does not matter in this case because you are not using the function the_excerpt() in this case, but it is something to be aware of. Your filter would not work at all if you used that function in your loop because you are basing it on the global $post value. In your loop over the posts of the post object field you are not altering the global $post and this means that you’re using the wrong post ID in your code. The function the_excerpt() only works on the global $post object.

  • @hube2 Thanks! That pointed me in the right direction. I made an amateur mistake and accidentally left off the parameter count on the filter. Once I added the 2 to the get_the_excerpt filter, the $post was passed in correctly and the custom excerpt value worked. Thanks again!

    add_filter( 'the_excerpt', 'my_theme_products_and_recipes_excerpt', 10, 1 );
    add_filter( 'get_the_excerpt', 'my_theme_products_and_recipes_excerpt', 10, 2 );
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.