2

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!

  • 1
    I can be totally wrong but is it not: $query->is_archive() ? – Charles Sep 30 '16 at 7:49
  • @charles, the link @Kare gave is for the single-matratze – bagpipper Sep 30 '16 at 7:57
2

Wordpress provides wp_get_recent_posts() function to retrive a number of recent posts from any post types. you can pass your custom post type as an arguments to retrieve recent posts lists.

$args = array(
    'numberposts' => '5',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'matratze',
    'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args );

Wordpress Core Recent Posts widget does not provide feature to list posts from custom post type. If you want to list post using Widget, you can use Posts in Sidebar Plugin. This plugins i very powerful and provides you many options to show posts based on many criteria including recent posts from custom post types.

UPDATE:

Here is code you can use for your specific case:

You are using Tab Widget to display recent posts which uses WP_Query. So you can use pre_get_posts to set post type filter.

function filter_recent_get_posts($query) {
    if (isset($_POST['tab']) && ($_POST['tab'] == 'recent')) {
        $query->set('post_type', 'matratze');
    }
}
add_action( 'pre_get_posts', 'filter_recent_get_posts' );
| improve this answer | |
  • Would you be so kind to pack this into a hook or filter? I would appreciate your reply! – Kare Sep 30 '16 at 18:57
  • @Kare I have updated answers with hook action you can use. – C Sabhar Oct 1 '16 at 9:08
  • Imho is there no reason to use a plugin for a simple thing like displaying CPT on Recent Post widget. – Charles Oct 1 '16 at 21:21
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

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.