• Theunknown1

    (@deexgnome)


    Hey,

    i trying to create a own custom excerp and save the value to the Meta of the post. My Goal overall is to save the first h2 to the meta and attach a dot to it.

    For the beginning i’m trying to save the auto excerp of wordpress to the meta without any filtering. But for some reason this field stays empty if i save the post. I hope that someone could tell me whats wrong with the script (functions.php).

    add_action( 'save_post', 'h2_excerpt_save' );
    function h2_excerpt_save( $post_id )
    {
        if ( $_POST['post_type'] == 'post' ) {
            add_post_meta($post_id, 'h2_excerpt_field', get_the_excerpt($post_id), true);
        }
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • nonsensecreativity

    (@nonsensecreativity)

    You should be using $_POST['excerpt'] not get_the_excerpt( $post_id )since the post haven’t been save yet so the excerpt value is not there yet

    Moderator bcworkz

    (@bcworkz)

    When ‘save_post’ fires, the post has been saved. Though it’s in $_POST['excerpt'] as well so that would work too, but only if you actually use the ‘excerpt’ edit screen field. Many do not, they let WP auto compose the excerpt or use the <!--more--> tag.

    get_the_excerpt() handles these variations, mainly because wp_trim_excerpt() is used to filter the output. What the real problem is is get_the_excerpt() is supposed to be used in the loop. It does not accept post ID arguments. You can simulate loop conditions by using the following code before calling get_the_excerpt():

    global $post;
    $post = get_post($post_id);
    setup_postdata( $post );

    Thread Starter Theunknown1

    (@deexgnome)

    Hi and thank for your answer?

    The code you post as example can be inside the function? Or must it be used somwhere else, i ran with some trying in several internal server error.

    Moderator bcworkz

    (@bcworkz)

    Yes, in a function. Sorry I wasn’t more explicit. This is what I had in mind:

    add_action( 'save_post', 'h2_excerpt_save' );
    function h2_excerpt_save( $post_id )
    {
        if ( $_POST['post_type'] == 'post' ) {
            global $post;
            $post = get_post($post_id);
            setup_postdata( $post );
            add_post_meta($post_id, 'h2_excerpt_field', get_the_excerpt(), true);
        }
    }

    Thread Starter Theunknown1

    (@deexgnome)

    Wow Thats working fine thanks.
    But can it be that it wont update the meta if i resave the Post?

    Moderator bcworkz

    (@bcworkz)

    It should update, but there could be an issue with if ( $_POST['post_type'] == 'post' ). If the $_POST array should be unavailable or altered some how, your update might not run.

    You could instead verify post type after setup_postdata( $post ); using $post->post_type. I’m not convinced this is the issue, but as a general rule I don’t like to rely on $_POST content any later than I have to.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Save custom Excerp to Meta’ is closed to new replies.