I have many hundreds of post which start with 'Post' and then a hyphen -

Some of these post have some other unimportant text before it, usually post summaries. What comes after the hyphen is the body of the post that I want to retain.

My objective is to have a solution that (ideally) automatically finds 'Post' at the beginning of the posts and then deletes all the text/everything that comes before it, including the word Post WITH the hyphen. So what remains will be the main post.

I have no idea how to approach this (very new to WP development).

  • can you post a fragment of the content? selecting the part you want deleted – David Lee Sep 22 '17 at 22:07
  • Here is an example: Summary: The neurological benefits experienced by those with epilepsy who follow ketogenic diets may be a result of the low carb diet lowering brain inflammation, researchers report. Source: UCSF. Post - Ketogenic diets......" As you can see, everything before 'Post - ' is what I want to delete and then the post can start which is the main body of the text. I don't care too much about loosing any text before the Post text. – JS7319 Sep 22 '17 at 23:47
  • @DavidLee This sounds like a good use for a filter hook if I am correct? Because the content needs to be changed and not an action needs to be performed. So I would look into filter hooks. codex.wordpress.org/Plugin_API/Filter_Reference I saw this youtube.com/watch?v=AJw72ZqhbQE and this youtu.be/-0jKrGVTs1w?t=8m21s. So make a filter that hooks into each post and does a regex for "Post -" then deleting all p divs till parent div, would that be the way to tackle this? – lowtechsun Sep 22 '17 at 23:58

Note that this filter function, added to the theme functions.php file, will apply to all output that uses "the_content" everywhere (all posts essentially) - so exercise or consider narrowing the scope.

For instance, you might want to limit it to the first 100 characters, or to posts before a certain date, or within a certain category before a certain date, etc., otherwise someday someone may happen to use the same sequence of characters and be shocked to find that everything up to them has apparently been deleted.

 * Filter all posts to remove 'Post - ' 
 * and all characters preceding it 
 * from all posts
add_filter( 'the_content', 'get_rid_of_text_to_post_dash' );

function get_rid_of_text_to_post_dash( $content ) {         

    $key_string = 'Post - ';
    $new_content = ( strpos( $content, $key_string ) ) ? 
        substr( $content, strpos( $content, $key_string) + 7 ) : 

    return $new_content;

  • For another user's posts to be deleted they will have to use "Post -" within their post content, but you're right, if I only modify it to search the first X number of characters of a post, the possibility of someone triggering the deletion will be avoided. Thank you for supplying the proposed code, but how and where do I use it? Can it be made into a plugin so it can be deactivated if required? And does it feature a character limit - say for the first 200? – JS7319 Sep 24 '17 at 3:22
  • It COULD be made into a plugin, but the easiest and most direct way to proceed - assuming you don't want to go into the database and attempt the operation described below - will be to 1) place the code in the theme functions.php file, and 2) within the function itself, deploy a conditional statement restricting application, perhaps,as suggested, by post publication date or category or some other appropriate combination. If you don't know how to do this, then you may need to hire a developer to do it and instruct you on how it's done - or search around for instructions. – CK MacLeod Sep 26 '17 at 19:46
  • I am happy to enter the functions.php file, insert or replace code, but the code which you've pasted above, is that something that will be needed to be inserted into the functions.php, with some amendment, to action my objective? – JS7319 Oct 9 '17 at 0:10
  • Can't say for 100% sure without seeing what else might conceivably be going on at your site, but it will probably work if it's a normal WP site without peculiar modifications. So, just dropped in the active theme's functions.php - I think it was Twenty Seventeen when I tested it, or some other standard WP theme - the above worked for me. Note: Be very careful editing functions.php. One of the easiest ways to crash a site is to make a typo there. – CK MacLeod Oct 9 '17 at 0:27

To save using extra filters it is best to completely remove the text. To do this open the websites database in phpmyadmin select all and download. once on a local machine open in notepad and do ctrl + h this allows you to find and replace just use this to remove all the unwanted text. once complete save it then go back to phpmyadmin to import the edited .sql database file. You will need to delete the tables in your database before you can import so make sure to download another copy of your database as a backup.

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.