0

I am making a booking site, where cabins are a custom post. Users pay with Woocommerce subscription to have their cabins visible on the site.

Therefore, I need to check if the user have an active subscription in order to publish the post(s) created.

Currently, I check if the user have an active subscription like so:

if (has_woocommerce_subscription('','','active')) {

While this works, it is clearly not changing the post status.

So my question is, how do I get the current user, the users posts id, the status etc., and change the post status to published?

I found this in another thread, although, it changes the post status from published to draft, so I'm guessing it could be similar?

add_action('publish_post', 'check_user_publish', 10, 2);

function check_user_publish ($post_id, $post) {

    $user_id = get_current_user_id();

    if ($user_id != $post->post_author)
        return;

    $query = array(
        'ID' => $post_id,
        'post_status' => 'draft',
    );
    wp_update_post( $query, true );

}}

Ideally, this check should be run maybe once a day if possible?

Thanks

1 Answer 1

0

The database holds several rows for each attribute of a post. All except on row should have status inherit. So I just hope the post_id is correct.

Anyway, yes you are correct to assume that it is similar:

$query = array(
  'ID' => $post_id,
  'post_status' => 'publish',
);
wp_update_post( $query, true );

Oh and

add_action('publish_post'...

adds the check when the post is being published (so no need to check once a day).

3
  • I may have explained myself poorly. At first, I wanted the check to be done at a user level, but thinking about it, it makes more sense to run it on all users once a day. Since it should also work the other way around, from published to pending if the subscription is run out.. But I guess it's an entirely different task than what I've asked here.
    – Beyond55
    Feb 9, 2017 at 12:35
  • WordPress runs all the functions when it's being used by you or your users. So when your users 'publish_post' the function check_user_publish is fired up. That way, the function is only called when a post is publish, and is only ran for the user that published the post. If you want to run it past all your users daily, the function will change significantly. Also you would not use add_action. Feb 16, 2017 at 12:07
  • Thank you for your time. I've decided to pay for help, creating the function to run once a day.
    – Beyond55
    Feb 20, 2017 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.