Add custom post type to Classic Recent Posts?

I am using the Classic Recent Posts element, and I have a custom post type for trainings. I would like to include my posts and trainings together in the recent posts. However, it is only only including posts. It doesn’t even show my trainings in the dropdown to choose from. I did eventually find the following in the forum answers from 2017, but it’s not working for me:

add_filter( 'cs_recent_posts_post_types', 'x_recent_post_types' );
function x_recent_post_types(){
    return array(
        'post' => 'post',
        'trainings' => 'trainings'
    );
}

Hello @tkulow,

Thanks for writing to us.

Regretfully there is no option to display custom post type post in the Classic Recent Post element. To display the custom post type post you can use the Looper where you need to set the Looper Provider as Query String and set the Post type that you want to set.

Test-Page-Builder-Pro (10)

Alternatively, you can use Recent Post Classic Shortcode where you can set Post type.

Hope it helps
Thanks

For whatever reason, I am not able to get your Looper suggestion to work. I did manage to find and use this code, which works on one of my sites with Cornerstone and not the other. Both are using the Classic Recent Posts element. I thought it was caching issue, but I have cleared cache from server, browser, and Cornerstone settings. Other things change just fine, but this is not working. Is there anything that you can see that might make it not work?

/**
 * Add post types to recent posts V3
 */
function x_shortcode_recent_posts_v3( $atts ) {
    extract( shortcode_atts( array(
      'id'           => '',
      'class'        => '',
      'style'        => '',
      'type'         => 'post',
      'count'        => '',
      'category'     => '',
      'offset'       => '',
      'orientation'  => '',
      // 'show_excerpt' => 'true',
      'no_sticky'    => '',
      'no_image'     => '',
      'fade'         => ''
    ), $atts, 'x_recent_posts' ) );
  
    $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 
        'post' => 'post',
        'trainings' => 'trainings' // <-- HERE
    ) );
    $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';
  
    $id            = ( $id           != ''     ) ? 'id="' . esc_attr( $id ) . '"' : '';
    $class         = ( $class        != ''     ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
    $style         = ( $style        != ''     ) ? 'style="' . $style . '"' : '';
    $count         = ( $count        != ''     ) ? $count : 3;
    $category      = ( $category     != ''     ) ? $category : '';
    $category_type = ( $type         == 'post' ) ? 'category_name' : 'portfolio-category';
    $offset        = ( $offset       != ''     ) ? $offset : 0;
    $orientation   = ( $orientation  != ''     ) ? ' ' . $orientation : ' horizontal';
    // $show_excerpt  = ( $show_excerpt == 'true' );
    $no_sticky     = ( $no_sticky    == 'true' );
    $no_image      = ( $no_image     == 'true' ) ? $no_image : '';
    $fade          = ( $fade         == 'true' ) ? $fade : 'false';
  
    $js_params = array(
      'fade' => ( $fade == 'true' )
    );
  
    $data = cs_generate_data_attributes( 'recent_posts', $js_params );
  
    $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" updated-by=\"ERI\">";
      
      $set_order = 'DESC';
      if ($category == 'scheduled') {
          $set_order = 'ASC';
      }
  
      $q = new WP_Query( array(
        'orderby'             => 'date',
        'order'               => $set_order,
        'date_query' => array(
          array(
              'column' => 'post_date_gmt',
              'after'  => '1 year ago',
          )
        ),
        'post_type'           => array('post', 'trainings'),  // <-- HERE
        'posts_per_page'      => "{$count}",
        'offset'              => "{$offset}",
        "{$category_type}"    => "{$category}",
        'ignore_sticky_posts' => $no_sticky
      ) );
  
      if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
  
        if ( $no_image == 'true' ) {
          $image_output       = '';
          $image_output_class = 'no-image';
        } else {
          $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
          $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
          $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
          $image_output_class = 'with-image';
        }
  
        // $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';
  
        $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( csi18n('shortcodes.recent-posts-permalink'), the_title_attribute( 'echo=0' ) ) ) . '">'
                   . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
                     . '<div class="entry-wrap">'
                       . $image_output
                       . '<div class="x-recent-posts-content">'
                         . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                         . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                         // . $excerpt
                       . '</div>'
                     . '</div>'
                   . '</article>'
                 . '</a>';
  
      endwhile; endif; wp_reset_postdata();
  
    $output .= '</div>';
  
    return $output;
}
add_action('wp_head', 'change_recent_posts_to_v3');
function change_recent_posts_to_v3() {
    remove_shortcode( 'x_recent_posts' );
    remove_shortcode( 'recent_posts' );
    add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v3' );
    add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v3' );
}

Hello @tkulow,

What you currently have is an outdated code. Be advised that custom coding is beyond the scope of our support already. And besides, the Classic Recent Posts element is only limited up to 4 items only.

We highly recommend that you use the new Posts element. You can use this element and modify the Looper Provider by switching it from Recent Posts to Query Builder so that you can include both the posts and trainings custom post types.

If you want us to give you a demonstration of how you can use the Loopers to display both the latest posts and training items, you may give access to your site so we can create an example. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
- Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.