Advertisement
tzvij

publish periodical post

Dec 1st, 2013
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. function mag_check_for_new_issue_on_publish( $post_id ){
  2.  
  3. // check that this is a real publish and not an update
  4. if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
  5. // is the post in the master category?
  6. if ( in_category( get_option( 'mag_master_category' ) , $post_id ) ) {
  7. // get the issue and assign this to global option current_issue
  8. $terms = get_the_terms ( $post_id , 'issue' );
  9. if ($terms) {
  10. update_option ( 'mag_current_issue' , $terms[0]->slug );
  11. }
  12. // publish all posts that are in the same issue
  13. $args = array(
  14. 'posts_per_page' => -1,
  15. 'post_status' => 'pending',
  16. 'tax_query' => array(
  17. array(
  18. 'taxonomy' => 'issue',
  19. 'field' => 'slug',
  20. 'terms' => $terms[0]->slug,
  21. )
  22. )
  23. );
  24. remove_action( 'publish_post' , 'mag_check_for_new_issue_on_publish' );
  25.  
  26. // get list of posts
  27. $issue_pending_posts = get_posts( $args );
  28.  
  29. // loop through and set each post_status to publish
  30. foreach( $issue_pending_posts as $pending_post ) {
  31.  
  32. $pending_post->post_status = 'publish';
  33. wp_update_post( $pending_post );
  34.  
  35. }
  36. add_action( 'publish_post' , 'mag_check_for_new_issue_on_publish' );
  37. }
  38. }
  39. }
  40.  
  41. // register the action
  42. add_action( 'publish_post' , 'mag_check_for_new_issue_on_publish' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement