Search code examples
sqlwordpressconcatenation

Wordpress post update code is not working


I want to update a topic with a post id in Wordpress and add it to the end of the post. However, I could not run the code below. Can you help?

wp_update_post(['ID' => $posts->ID,"post_content" => "concat_ws(' ',post_content, 'SECOND')"]);

Normally, this process is done over sql with concat. But I want to use it with php.

The version that works with sql;

update test_user set descrip = concat_ws(' ',descrip, 'SECOND') where Id=2

but I want to run it with php, not sql. How should the first code be?


Solution

  • You can use braces or concatenation operator .

    echo "qwe{$a}rty"; // qwe12345rty, using braces
    echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
    

    Also, it much better to use WP_Post class than modify data in tables directly. Your WP instance can use some db caching layer, or some hooks for posts updating. This functionality can be potentially broken if you work with tables directly.

    $post = get_post( 123 ); 
    $post->post_content = $post->post_content . "Some other content"; 
    wp_update_post( $post );