Skip to content

Instantly share code, notes, and snippets.

@TimaGribanov
Last active May 6, 2025 14:08
Show Gist options
  • Save TimaGribanov/1f98fad1c38ba7bd908feb1df3caa83a to your computer and use it in GitHub Desktop.
Save TimaGribanov/1f98fad1c38ba7bd908feb1df3caa83a to your computer and use it in GitHub Desktop.
Add a paragraph before 'read more' as an excerpt to a WordPress post
/*Select the needed part (excluding <p> tags and <!-- wp:paragraph --> line*/
SELECT
REPLACE(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(post_content, '\n', 2), '\n', -1), '<p>', ''), '</p>', '') as excerpt
FROM
wp_posts wp
WHERE
ID = 1771;
/*Insert that part as an excerpt for a certain post*/
UPDATE
wp_posts
SET
post_excerpt = (
SELECT
REPLACE(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(post_content, '\n', 2), '\n', -1), '<p>', ''), '</p>', '')
FROM
wp_posts
WHERE
ID = 1771)
WHERE
ID = 1771;
/* Insert the excerpt for all posts without one */
UPDATE
wp_posts wp
JOIN (
SELECT
ID as id,
REPLACE(REPLACE(SUBSTRING_INDEX(SUBSTRING_INDEX(post_content, '\n', 2), '\n', -1), '<p>', ''), '</p>', '') as excerpt
FROM
wp_posts
WHERE
post_status = 'publish'
AND post_excerpt = ''
AND post_type = 'post'
) a
ON
wp.id = a.id SET
wp.post_excerpt = a.excerpt;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment