milardovich

Wordpress check new arrivals category

May 3rd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. function add_new_arrival_meta( $post_id ) {
  2.  
  3.     // If this is just a revision, don't check.
  4.     if ( wp_is_post_revision( $post_id ) )
  5.         return;
  6.  
  7.     // Get all categories and look if the one with the slug new-arrivals is there
  8.     $categories = wp_get_post_categories($post_id);
  9.     $is_new_arrival = array_filter(
  10.       $categories,
  11.       function ($e) {
  12.         return $e->slug == 'new-arrivals';
  13.       }
  14.     );
  15.  
  16.     // If it is there, then add a post meta
  17.     if($is_new_arrival)
  18.         add_post_meta($post_id,'arrived_at',date('Y-m-d'));
  19. }
  20. add_action( 'save_post', 'add_new_arrival_meta' );
Advertisement
Add Comment
Please, Sign In to add comment