How to include content from multiple post types in the same slider.

jeff.sydor's Avatar

jeff.sydor

17 Jan, 2020 08:58 PM

Here's my demo site: http://dev.buchanan-ip.com/

Normally, my users would just include content that are standard posts and these would be checked off as "Homepage Slider" in the post and these would be added to my Posts Slider. In this case, the users want to have announcements that would also be featured in the slider. Since I don't want these to show up in my posts archive, I created a custom post type called "Announcements". Now, I can target these posts or normal blog posts. But not both.

There are two options I was considering, but I'm not sure how to implement them. Unless there is another option that I'm not aware of.

1) adding the "slider" taxonomy to my register_post_type array. Similarly to how I would add categories to my custom post type.
$args = array( 'taxonomies' => array('category') )
Is there a parameter to use in this way to include the slider options from normal post types?

2) creating a second slider and combining the results into one.
In my php file, i have <div class="largeSlider"><?php echo get_new_royalslider(1); ?></div>
Is there a way to take the results from get_new_royalslider(1) & get_new_royalslider(2), whereas #1 is the traditional post content and #2 is the custom post content, and combine them into a single slider? I tried something like <div class="largeSlider"><?php echo get_new_royalslider(1) . get_new_royalslider(2); ?></div> but obviously this just created two separate sliders on top of one another.

I'd really hate to have to add the advisories as a custom taxonomy in my regular posts as my way keeps things cleaner for the users. But If this makes sense to you, I'd really appreciate any help.

Thank you very much!

  1. Support Staff 1 Posted by Dmitry Semenov on 18 Jan, 2020 02:52 PM

    Dmitry Semenov's Avatar

    Hello,

    The plugin has filter that allows to fully override query or add more posts to existing one, please refer to http://help.dimsemenov.com/kb/wordpress-royalslider-advanced/wp-mod...

  2. 2 Posted by jeff.sydor on 21 Jan, 2020 09:47 PM

    jeff.sydor's Avatar

    That worked BEAUTIFULLY!

    Thank you so much!

  3. 3 Posted by jeff.sydor on 21 Jan, 2020 09:56 PM

    jeff.sydor's Avatar

    Actually, I'm just noticing that the additional slides from my custom post type is getting added after the standard post content regardless of the published date of the content. How can I fix this?

    Thanks

  4. Support Staff 4 Posted by Dmitry Semenov on 21 Jan, 2020 10:24 PM

    Dmitry Semenov's Avatar

    Slider refreshes at a given interval, you may adjust cache refresh time on RoyalSlider Settings page (to disable set to 0).

  5. 5 Posted by jeff.sydor on 22 Jan, 2020 07:24 PM

    jeff.sydor's Avatar

    It already is set to zero.

    Is there something in my code that needs adjusted?

    function add_additional_posts_to_slider($slides, $options, $type) {
      // $options['id'] is a slider index


    $args = array( 'post_type' => 'announcement-slides' ); $query = new WP_Query( $args );


    $additiona_slides = ( array )$query->posts; if ( is_array( $additiona_slides ) ) { $slides = array_merge( $slides, $additiona_slides ); } return $slides; // slides is array of WP Post objects that will be displayed in slider } add_filter( 'new_rs_slides_filter', 'add_additional_posts_to_slider', 10, 3 );

    The content in the slider here: http://dev.buchanan-ip.com/ shows 3 slides, but the 3rd one (announcement) was published later, so you'd think it should be at the top of the list.

  6. Support Staff 6 Posted by Dmitry Semenov on 22 Jan, 2020 07:30 PM

    Dmitry Semenov's Avatar

    Ah, I misunderstood your question. The code that you're using just adds posts after the current ones.

    Instead of merging slides you may build completely custom query, e.g.:

    $args = array(
      'post_type' => array('announcement-slides', 'some-other-post-type'), 
    );
    $query = new WP_Query($args);
    $slides = (array)$query->posts;
    return $slides;
    
  7. 7 Posted by jeff.sydor on 22 Jan, 2020 08:39 PM

    jeff.sydor's Avatar

    Ok that makes sense but i'm still a little confused. How do i also target posts that are assigned to your 'slider' taxonomy under posts? In this case, I have a slider called "homepage slider" designated in there. Is there a post_type that targets that?

  8. Support Staff 8 Posted by Dmitry Semenov on 23 Jan, 2020 05:35 AM

    Dmitry Semenov's Avatar

    There is another filter that allows to modify existing query instead of replacing or "adding to" it - http://help.dimsemenov.com/kb/wordpress-royalslider-advanced/wp-mod...

    And if you have multiple different sliders and you want to apply the code to a specific one - there is $index or $options['id'] which is ID of the slider that is being filtered

  9. 9 Posted by jeff.sydor on 23 Jan, 2020 04:27 PM

    jeff.sydor's Avatar

    Sorry, but that just took me back to the original problem with the array_merge.

    Normally, I had standard blog posts that were tagged as "Homepage" under the Slider taxonomy that was created with the plugin. Now, I have my custom post type announcement-slides that I need added to the slider but have everything organized by publish date. I can't use the solution in #6 above because replacing 'some-other...' with 'posts' will add all blog posts. If I add 'tax_query' to $args in that example, it ignores my custom post type.

    I've tried the solutions you've linked to and some of my own and nothing works. They either break the plugin or arrange all custom post types before or after all the blog posts.

    Here's where I left off. I was trying to pull in both post types in separate queries, merge them and display them by date. I end up with an error that no posts are found. But if I just target $args1 or $args2, I get those respective results. I'm not sure what else to do now:

    function add_additional_posts_to_slider($slides, $options, $type) {
    
      $args1 = array(
        'post_type' => array('announcement-slides')
      );
      
      $args2 = array(
        'tax_query' => array(
          array(
            'taxonomy' => 'slider',
            'field' => 'slug',
            'terms' => 'homepage',
          )),
      );
      
      $ids1 = array();
      $ids2 = array();
      
      $loop1 = new WP_Query( $args1 );
      while ( $loop1->have_posts() ) : $loop1->the_post();
        array_push($ids1, get_the_ID());
      endwhile;
      
      $loop2 = new WP_Query( $args2 );
      while ( $loop2->have_posts() ) : $loop2->the_post();
        array_push($ids2, get_the_ID());
      endwhile;
      
      // merge args
      $args = array_merge($ids1,$ids2);
      
      // the main query
      $query = new WP_Query(array(
        //'post_type' => array('announcement-slides', 'posts'),
        'post__in' => $args,
        'orderby' => 'date',
        'order' => 'DESC',
      ));
      
      $slides = (array)$query->posts;
      
      return $slides; // slides is array of WP Post objects that will be displayed in slider
    }
    add_filter( 'new_rs_slides_filter', 'add_additional_posts_to_slider', 10, 3 );
    
  10. Support Staff 10 Posted by Dmitry Semenov on 23 Jan, 2020 05:32 PM

    Dmitry Semenov's Avatar

    I don't see any mistakes in code that you posted at first glance, all I can recommend is to keep debugging.

    Also, you can query just ids via WP_Query https://developer.wordpress.org/reference/classes/wp_query/#return-... , so you don't have to mess with loops.

    $ids_query = new WP_Query(array(
       'post_type' => array('announcement-slides'),
        'fields' => 'ids'
    ));
    var_dump($ids_query->posts); // ids
    
  11. 11 Posted by jeff.sydor on 23 Jan, 2020 05:41 PM

    jeff.sydor's Avatar

    I'll try what you just added. The problem with my code I added was that only the blog posts from $args2 was returning. The custom post type was excluded.

  12. 12 Posted by jeff.sydor on 23 Jan, 2020 11:43 PM

    jeff.sydor's Avatar

    Ok so i'm not sure exactly what I did differently here, but I have a working function! Below is what I did if you want to cannibalize it and use it in your documentation.

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


    // Create blank arrays $ids1 = array(); $ids2 = array();


    // The 1st Query $query1 = new WP_Query(array( 'post_type' => array('announcement-slides'), 'fields' => 'ids', ));


    // The 1st Loop while ( $query1->have_posts() ) : $query1->the_post(); array_push($ids1, get_the_ID()); endwhile;


    wp_reset_postdata();


    // The 2nd Query $query2 = new WP_Query(array( //'post_type' => array('posts'), 'tax_query' => array( array( 'taxonomy' => 'slider', 'field' => 'slug', 'terms' => 'homepage', )), 'fields' => 'ids', ));


    // The 2nd Loop while ( $query2->have_posts() ) : $query2->the_post(); array_push($ids2, get_the_ID()); endwhile;


    wp_reset_postdata();


    // Merge Arrays $args = array_merge($ids1,$ids2);


    // The Main Query $query3 = new WP_Query(array( 'post_type' => 'any', 'post__in' => $args, // the array of ids created above will be included here regardless of the order 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 10, // Default maximum is 5; Change this number to the desired results


    ));


    $slides = (array)$query3->posts;


    return $slides; // slides is array of WP Post objects that will be displayed in slider


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

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac