Add scheduled custom posts for Royal Slider

The default flow for adding a new post to Royal Slider is:

  1. Create and publish your new post
  2. Go into the Royal Slider-plugin and re-save your slider

This isn’t very efficient, especially when you want to work with scheduled posts our even when working with different user roles (e.g. user without access to full website).

There are hooks to update your slider when you save a post or during many other action.

Extending this function to add custom posttypes with custom fields, we wrote this code:

function add_additional_posts_to_slider($slides, $options, $type) {

  // $options['id'] is the slider index

  if($options['id'] == 1) {

    $args = array(

      'post_type' => '<custom_post_type>',

      'post_status' => 'publish'

      );

    $custom_slides = get_posts($args);

    $additiona_slides = [];



    foreach ($custom_slides as $custom_slide_object) {

      $additiona_slides[] = array(

         'title' => get_field('slider_title', $custom_slide_object->ID),

         'description' => get_field('slider_description', $custom_slide_object->ID),

         'link' => get_field('slider_link', $custom_slide_object->ID),

         'image' => array('attachment_id' => (string)get_field('slider_image', $custom_slide_object->ID)['id'])

        );

  }



  if( is_array($additiona_slides) ) {

      $slides = array_merge($slides, $additiona_slides);

  }

}

  return $slides; // slides that will be displayed in slider

}

add_filter('new_rs_slides_filter', 'add_additional_posts_to_slider', 10, 3);

Our solution makes it possible for our client to create slides from a custom posttype and when the post changes from ‘Schedule’ to ‘Publish’ the function does the rest.

Plugins: