Last active
May 6, 2025 14:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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