Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1.  
  2.  
  3.  
  4. // Schedule Cron Job Event
  5. // Custom Cron Schedule Interval
  6.  
  7.  
  8.  
  9. function auto_post_job_function_own( $schedules ) {
  10.  
  11.     $schedules['post-change-24-hour'] = array(
  12.          'interval'  => 12,  // 86400
  13.         'display'   => __( 'Custom Label' )
  14.     );
  15.  
  16.     return $schedules;
  17. }
  18. add_filter( 'cron_schedules', 'auto_post_job_function_own' );
  19.  
  20. if ( ! wp_next_scheduled( 'auto_post_job_function_hook' ) ) {
  21.     wp_schedule_event( current_time( 'timestamp' ), 'post-change-24-hour', 'auto_post_job_function_hook' );
  22. }
  23.  
  24. add_action('auto_post_job_function_hook', 'auto_post_job_function');
  25.  
  26.  
  27. function auto_post_job_function() {
  28. $args = array(
  29.     'post_type' => 'post',
  30.     'orderby'   => 'rand',
  31.     'posts_per_page' => 1,
  32.     );
  33.  
  34. $the_query = new WP_Query( $args );
  35.  
  36. if ( $the_query->have_posts() ) {
  37.  
  38. $string .= '<ul>';
  39.     while ( $the_query->have_posts() ) {
  40.         $the_query->the_post();
  41.         $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
  42.     }
  43.     $string .= '</ul>';
  44.     /* Restore original Post Data */
  45.     wp_reset_postdata();
  46. } else {
  47.  
  48. $string .= 'no posts found';
  49. }
  50.  
  51. return $string;
  52.  
  53. };
  54.  
  55.  
  56. add_shortcode( 'rand_post', 'auto_post_job_function' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement