Display Custom Post Type in Recent Posts

I have the following problem:

I have created a custom post type matratze and currently besides the custom posts no other posts exist. Hence, nothing is being displayed under the recent posts.

The custom post type looks like the following:

[custom post]

My recent posts do not get displayed:

[main homepage]

I tried the following:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'matratze' ) );

    return $query;
}

Any suggestions what I am doing wrong?

I appreciate your replies!

2 Answers
2

To display Custom Post Types in the regular Recent Post widget for sidebar(s),
we use following function which works flawless for us.
We use our own functions and try to prevent overhead often created by plugins.

Note: make a backup before adding this function into functions.php

/**
 * Display CPT on Recent Post widget
 *
 * @version WP 4.6.1
 */
add_filter( 'widget_posts_args', 'wpse241060_widget_recent_post_4_cpt' );
function wpse241060_widget_recent_post_4_cpt( $params )
{
    $params['post_type'] = array( 'post', 'cpt01', 'cpt02');
    return $params;
}

Read more in Codex

Leave a Comment