0

I have a function that publishes a post if it exists as a draft. But the wp_update_post function seems to ignore the if/else and runs before anything else.

        $post = get_posts(array(
            'post_type' => 'post',
            'post_status' => array('publish', 'draft'),
            'meta_query' => array(
                array(
                    'key' => 'unique_post_meta',
                    'value' => $_GET['unique_post_meta']
                )
            )
        ))[0];

        if($post){
            $post_status = $post->post_status;

            if($post_status == 'publish' && $post_status != 'draft'){
                echo 'Post is already published';
            }
            else{

                if($post_status == 'draft'){
                    $post_updated = wp_update_post(array(
                        'ID' => $post->ID,
                        'post_status' => 'publish'
                    ));
                    if($post_updated){
                        echo 'Post just got published';
                    }
                    else{
                        echo 'Error';
                    }
                }
            }
        }
        else{
            echo 'Post doesn't exist';
        }

When the post is a draft, I still get the "post is already published" response. Even though it should have run wp_update_post and echoed "post just got published". It seems to run wp_update_post, but then jumps out of the if/else and checks again and now echoes "post is already published". How is this possible?

When I comment out wp_update_post it echoes "Error".

I'm clearly missing something...

  • 1
    Which hook are you using when executing this functions? – kierzniak Dec 28 '17 at 13:06
  • the function is in a shortcode that runs on a page. – kylmark Dec 28 '17 at 13:19
  • It seems to run wp_update_post: Do u mean, the post sts changes from draft to published even though the output is post is already published? – Vivek Athalye Dec 28 '17 at 13:21
  • Maybe you are first updating post and than try to update it? Shortcode should not be used to update post. Try to move your update code to init hook and leave display code shortcode. – kierzniak Dec 28 '17 at 13:22
  • Yes, the post status changes from draft to publish and I get the message "post is already published", which I should only get if wp_update_post didn't run. – kylmark Dec 28 '17 at 13:24
0

How is this possible?

I can think of one possibility that for some reason ur page is getting auto reloaded. So the flow of execution is like: you access the page, your code gets executed changing status from draft to published, then some other code gets executed and the page is reloaded. This time when your code gets executed it sees the post is already published and outputs the msg accordingly.

Along with echo statements try using error_log as well and see what you get in the log.

Also you can check developer tools > network tab in your browser if the page is getting auto reloaded or not.

  • I dont really see how/why the page would get reloaded. I guess running this code in a shortcode and after an ob_start() might be what's messing it up... – kylmark Dec 28 '17 at 13:31
  • 1
    try terminating the loading process after your code. this will give you the very first o/p. – Sid Dec 28 '17 at 13:33
  • Plz check the updated answer. See if error_log helps you find the rootcause. – Vivek Athalye Dec 28 '17 at 13:34

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.