• Resolved rexsorgatz

    (@rexsorgatz)


    Hard one:

    I have a site with thousands of posts that contain a hard-coded subhead at the top of the body content. That is, every posts starts something like this:

    <h2>Specific subhead related to this post</h2><p>Here is where the post starts…</p>

    I would like to extract that data (“Specific subhead related to this post”) from the post content and place it into a custom field instead. Some variation of Regex and SQL is likely required, but it would be helpful if there were some code base I start from…

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry, no base script, but the concept is not that complex. This is assuming that once the sub head is stripped out, the regexp will not match the content any more. Two <h2> blocks in a row would be a problem.

    You first query for posts where content matches the regexp. Use LIMIT to only get a number of posts that can reasonably be processed at one time. Run a foreach loop to strip out the subhead and place it in post meta. Update the new post content each time.

    Finally, schedule a recurring task to run the above script every so often. Add some more code so the recurring task is removed if the query is not finding any more matches.

    If two <h2>s in a row do occur, you could replace the first occurrence with &zwnj; (zero width non joiner) to prevent another regexp match, assuming your regexp is something like ‘^<h2>.*<\/h2>’

    Thread Starter rexsorgatz

    (@rexsorgatz)

    Thanks, bcworkz.

    Once the regex captures the data, any idea how to programmatically insert that into a new custom field? I have no idea what that code looks like.

    Moderator bcworkz

    (@bcworkz)

    Something like this should work, assuming the current post object in your loop is $post (untested):

    preg_match('/(^<h2>.*<\/h2>)(.*)/i', $post->post_content, $matches);
    // custom field is the same as post meta
    add_post_meta( $post->ID, 'subhead', $matches[1]);
    $post->post_content = $matches[2];
    wp_update_post( $post );

    Thread Starter rexsorgatz

    (@rexsorgatz)

    Perfect.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Extract Post Content into Custom Field’ is closed to new replies.