Real Noob
Real Noob

Reputation: 1593

Update WordPress Post Excerpt programmatically

I am looking for a function that can update the excerpts of already published WordPress posts. The function wp_update_post() does not update the excerpts. Other functions that I looked into like the_excerpt() are only for getting the excerpt instead of setting it.

How can I update the excerpts of published posts in WordPress?

Upvotes: 5

Views: 6862

Answers (2)

Med HIJAZI
Med HIJAZI

Reputation: 1

if you want to update all post you can do this:

$posts = get_posts( array( 
    'post_type'      => 'post', 
    'posts_per_page' => 750, 
    'offset'         => 0, 
) );

foreach( $posts as $post ):
    // Update each post with your reg-ex content filter:
    $pid = wp_update_post( array( 
        'ID'           => $post->ID,
        'post_excerpt' => '',
    ) );
    // Show the update process:
    printf( '<p>Post with ID: %d was %s updated</p>', 
        $post->ID, 
        ( 0 < $pid ) ? '' : 'NOT' 
    );     
endforeach;

Upvotes: 0

user3934058
user3934058

Reputation:

wp_update_post() should be the choice to use.

You can target the post_excerpt, in the $args, to address the Posts Excerpt.

An (untested, but should work) example:

$the_post = array(
      'ID'           => 37,//the ID of the Post
      'post_excerpt' => 'This is the updated excerpt.',
  );
wp_update_post( $the_post );

This is a very undynamic example but should give an idea about how to do it.

Upvotes: 12

Related Questions