1

I actually can show content and change content while displaying post. Problem is that I am using RSS importer and after importing I want to make search and replace and correct data imported because I know what is wrong with it.

    <?php
      add_action('pmxi_saved_post', 'post_saved', 10, 1);
      $my_post = array(
          "ID"           => $id,
          "post_content" => "1",
      );
     wp_update_post( $my_post );
    ?>

this is code I am using. Problem is that "post_content" does not change to value 1. Basically this code should change whatever is in post $id to value "1".

Why is this not working? I cannot figure it out for 2 hours now. All examples I have are based on change content while displaying but that does not change content IN database, only what is shown.

  • Would you update your answer to include the pmxi_saved_post() function? The code in your question is incomplete. – Dave Romsey Nov 20 '16 at 22:56
  • @DaveRomsey can I post link to Page with reference for this example? Actually this pxmi_saved_post is from plugin. I think it is not relevant to this problem. I just want to save value. How? – BSheep Nov 20 '16 at 23:03
  • Okay, I'd imagine it's just going to fire when saving an imported post. On second thought, what we need is your post_saved function. – Dave Romsey Nov 20 '16 at 23:13
2

From the documentation of WP All Import, the pmxi_saved_post action takes one parameter, $id, which is "the ID of the post/page/Custom Post Type that was just created."

So, your post_saved() callback should look like this:

add_action('pmxi_saved_post', 'wpse246838_post_saved', 10, 1);
function wpse246838_post_saved( $id ) {
    $my_post = array(
            "ID"           => $id,
            "post_content" => "1",
    );
    wp_update_post( $my_post );
}
  • You got it, @BSheep! Glad we were able to solve it! – Dave Romsey Nov 21 '16 at 0:04
  • 1
    PS: I'm surprised that the documentation would suggest such a general function name as post_saved. We should consider using a namespace, prefixing it or using anonymous functions, to avoid possible name collisions. – birgire Nov 21 '16 at 0:05
  • 2
    @birgire I agree, and that was lazy of me not to use something more appropriate. Updated the answer with the good old wpse{question_id}_ prefix. – Dave Romsey Nov 21 '16 at 0:13

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.