0

Custom Post type feed url showing the main feed's content.

Details: i have some custom post type on my site. I added them to main feed using this snippet

function mycustomfeed_cpt_feed( $query ) {

  // Add to my custom-type post type
  $types = array('post','blood-blog');

        if ( $query->is_feed() )
            $query->set( 'post_type', $types ); 
        return $query;
}
add_filter( 'pre_get_posts', 'mycustomfeed_cpt_feed' );

Here is how I registered the post type

/*Register Blood Blog*/
function mcp_blood_blog() {
    $labels = array(
        'name'               => _x( 'Blood Blog', 'post type general name' ),
        'singular_name'      => _x( 'Blood Blog', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'Blood Blog' ),
        'add_new_item'       => __( 'Add New Blog' ),
        'edit_item'          => __( 'Edit' ),
        'new_item'           => __( 'New Blood Blog' ),
        'all_items'          => __( 'Blood Blog' ),
        'view_item'          => __( 'View Blog' ),
        'search_items'       => __( 'Search Blog' ),
        'not_found'          => __( 'No Blog Found' ),
        'not_found_in_trash' => __( 'No Blog Found Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Blood Blog'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Insert A Blood Blog',
        'public'        => true,
        'menu_position' => 5,
        'menu_icon'     => 'dashicons-heart',
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'author' ),
        'has_archive'   => true,
        'hierarchical'  => false,
        'rewrite'       => array('slug' => 'blood/blog','with_front' => true,'feeds' => true,),
        'query_var'     => true,
        'publicly_queryable'    => true,
        'public'                => true,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'capability_type' => 'post',
        '_edit_link' => 'post.php?post=%d' ,
    );
    register_post_type( 'blood-blog', $args );
}
add_action( 'init', 'mcp_blood_blog' );
/*End Blood Blog*/

The problem is when i try to get the feed from custom post type like this one:

http://www.mediwaretesting.com/blood/blog/feed/

it returns the main feed content that already in

http://www.mediwaretesting.com/feed/

note: "blood/blog" in post type slug.

0

Note that $query->is_feed() is true for both feeds, so you're changing both of them with the pre_get_posts hook.

I skimmed quickly through the WP_Query object for those feeds and if you want to combine these two post types, only for the main feed example.tld/feed/, you could try:

add_filter( 'pre_get_posts', function( \WP_Query $q )
{
    if ( 
            $q->is_feed() 
         && ! $q->is_archive() 
         && ! $q->is_singular() 
         && ! $q->is_comment_feed() 
    )
        $q->set( 'post_type', [ 'post', 'blood-blog' ] );       
} );

I might be missing some checks, so you might want to double check it.

ps: it would be nice to have is_feed_home() or is_main_feed() here ;-)

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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