Problem: I would like to programmatically change the post status of all posts by the current User in a PHP hook I have set up to handle User role changes.
Solution: You can use the WordPress get_posts function to query all the author's posts, then update each of the posts as needed with wp_update_post. Here is a basic example:
... $user->set_role( 'tsn_bronze' ); // get all the published Voyage posts by the current user $args = array( 'post_type' => 'voyage', 'post_status' => 'publish', 'author' => $user_id, 'posts_per_page' => -1); $posts_array = get_posts($args); // loop over those posts and update each one to status "pending" foreach ($posts_array as $the_post) { $update_post = array( 'ID' => $the_post->ID, 'post_status' => 'pending' ); wp_update_post( $update_post ); }
If you want to use the same code to update posts of more than one custom post type, modify the 'post_type' argument to use an array instead of a single string:
$args = array( 'post_type' => array('voyage','deck-event'),
Relevant Documentation:
https://codex.wordpress.org/Template_Tags/get_posts
https://codex.wordpress.org/Function_Reference/wp_update_post
https://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Our next available supporter will start replying to tickets in about 0.74 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years, 9 months ago.
Assisted by: Christian Cox.