0

I've created a hierarchical post type called mycustomposttype. Parents are created in the backend, children via gravityforms.

Due to my theme setup, I would like all parents in mycustomposttype to display the shortcode [mycustomshortcode] in the excerpt.

To start, I've tried to auto-populate the excerpt for all mycustomposttype posts.

add_filter( 'get_the_excerpt', function( $post_excerpt, $post ){
if( $post->post_type != 'mycustomposttype' ) 
return $post_excerpt; 
return '[mycustomshortcode]';
 }, 99, 2 );

This does not produce any excerpt when creating or editing posts.

  1. how do I add a default excerpt value to all new posts of a custom post type?
  2. how do I add the default value to only all new parents of that custom post type?

Thanks.

2
  • 1
    If you're using a custom shortcode would it not be easier to just call the function that implements the shortcode directly? Why bother with the shortcode at all if it's just going to get parsed and turned back into the function call anyway. It's wasteful, similar to writing down notes for yourself by putting them in the mail and waiting for them to be delivered back to you. Note that get_the_excerpt only runs at runtime, not on update/save and has no impact on the editor – Tom J Nowell Sep 7 '20 at 22:51
  • @dkrahl check the response this should resolve your issue. Don't forget to let us know if it works and to accept answer if it does (so others can benefit as well) – sMyles Sep 13 '20 at 23:09
0

This will default the excerpt value in your custom post type only for new posts:

add_filter( 'default_excerpt', 'smyles_default_custom_post_excerpt', 10, 2 );

/**
 * Default Excerpt for Custom Post Type
 *
 * @param string  $post_excerpt Default post excerpt.
 * @param WP_Post $post         Post object.
 *
 * @return string
 *
 */
function smyles_default_custom_post_excerpt( $post_excerpt, $post ){
    
    if( $post && $post->post_type === 'mycustomposttype' && ! $post->post_parent ){
        return '[mycustomshortcode]';
    }
    
    return $post_excerpt;
}

The check for $post->post_parent checks the parent, if it is a "child" post, the value will be different than 0 which means it's the parent post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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