WordPress help

Dear Lazyweb, what's the WordPress call to turn comments on for a given post?

My wppost script posts with comments off until I go and hit Edit then Save. What am I missing?

Tags: , ,

13 Responses:

  1. Eric TF Bat says:

    I had a look at the source of the the wp_insert_post function, and it all looks to be in order. They do an ugly trick with converting an array to and from local variables, but that's not as bad as it could be, especially by PHP standards. Basically, it should be taking your comment_status value and accepting it, so if it's not happening properly then there's something deeply weird going on.

    Not very helpful, I know, but at least you can be slightly more confident that you're not missing something stupidly obvious that will make the entire intarwob laugh at you.

  2. Otto says:

    Since you're setting the $post['comment_status'] to 'open', comments should be open from the moment you create the post. There's nothing more to it than that, really.

    Normally, if you leave the comment_status out entirely, it uses the default setting from Settings->Discussion.

  3. Eric TF Bat says:

    What if you do something like

    wp_update_post(array('ID' => $id, 'comment_status' => 'open'));

    ... immediately after the wp_publish_post() call? I mean, it should be redundant as Otto says, and yet...

  4. Otto says:

    Found it. The comment_status is getting set to open, but the post_date_gmt is not set.

    WordPress stores two dates with every post. The post_date which is in local time, and the post_date_gmt which is in UTC/GMT time. The post_date seems to be getting automatically set, but post_date_gmt is not. This doesn't normally happen with a post because the post_date and post_date_gmt are set in the $post object by the admin code.

    Anyway, on Settings->Discussion, there's an option to automatically close comments on articles older than XX days. Because the post_date_gmt is not set, then it's thinking your post was created at the beginning of time, and so it's closing the comments. When you save, the post_date_gmt gets set correctly, and voila.

    Solution: Include the post date properly. Here's how:

    $post['post_date'] = date('Y-m-d H:i:s');
    $post['post_date_gmt'] = gmdate('Y-m-d H:i:s');

  5. Otto says:

    Note that your script also doesn't set a post_name, which might be a problem for posts displaying properly in some places, since the post name is used in the URL (like this post's post_name is "wordpress-help". The post_name is often called the "slug".

    • jwz says:

      Ugh, now I see that I'm also not getting mailed comments to these posts, though they show up in the admin page's list, and on the blog itself. I imagine there's something else unset... Is there an easy way to determine all of the fields that I am actually required to set to make things work? Is that way "run sql queries and compare good and bad posts" or is there a simpler approach?

      • Otto says:

        I think that the post_name is the only missing piece left there. On my test site, setting that was enough to make it look proper in the database. You can fill in a post name like so:

        $post['post_name'] = sanitize_title($subj);

        BTW, the parameter you send to --user should be a number, the ID of the user in the wp_users table. Not a name. If you're using a name there, that would cause the author to be wrong and thus for you to not get emails on comments.

        • jwz says:

          Yeah, I dunno; I'm setting the post_name but I'm not getting mailed replies. I guess I'll do some tests when I get home and I don't have to do it through the world's slowest ssh connection from an iPad...

          • Otto says:

            Looking through the comment notification code, it appears that, other than the notification setting being enabled, the main thing that the function relies on having is the post_author being the ID number of a user, and that the user has an email address set for them. If that is the case, the email pretty much has to be sent, there's no failure paths in there.

            So first thing to check is to make sure that --user is a valid user id number. If you pass in a string there, the resulting post will get a post_author of zero and thus no comment emails will get sent.

          • Otto says:

            Now that I take a closer look at your feed, I'm certain that that is your problem. The feed normally contains the author name in the dc:creator tag, but all your image posts lately (save a few) have no value there. The author isn't being set correctly.

            The --user parameter has to have a number, not a login name.

            • jwz says:

              Yeah, that seems to have been the problem, thanks! Now I'm doing $user = get_userdatabylogin($user)->ID.

              BTW, I had to do this to get the posts to not come out with post_date and post_date_gmt being the same:
              date_default_timezone_set (get_option ("timezone_string"));
              Apparently something under wp-settings.php sets the TZ to GMT.

              (Ideally I'd like the post_date to be the local-time reflected in the Date: header of the incoming post email (Central), rather than the default timezone of the blog (Pacific), but that's not something that's easy to do with strtotime()...)