Question

When I call the following php file, I want the code to:

  1. Get the latest blog post
  2. Append it with some text
  3. Save the appended blog post

The following code somewhat works in-so-far-as it will do steps 1 and 2 but will not save the contents as appended. Any help is appreciated! Thanks, novice wp developer.

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/blog/wp-load.php' );
$wp_did_header = true;

$args = array( 'numberposts' => 1 );
$recent_posts = get_posts( $args );

foreach($recent_posts as $post)
{
    $post->post_content .= '<br />Some text appended to Post';
    $post->post_content = add_filter('the_content', $post->post_content);
    return $post->post_content;
}
?>

No correct solution

OTHER TIPS

add_filter accepts second parameter as callback function name not just string.

return cannot be call inside loops like you did. It will just stop executing script.

return only be declared inside functions/methods

Your loop will be like this:

foreach($recent_posts as $post)
{
    echo $post->post_content . '<br />Some text appended to Post';
}

Actually you don't have to apply any filter here. Just append the $post property post_content with your value.

You don't need use filters, just display text after the post

  foreach ($recent_posts as $post) :
  setup_postdata($post);
  the_content();
  echo '<br />Some text appended to Post';
  endforeach;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top