1

I have a question about WordPress feeds.

I know that the default feed:

http://www.example.com/feed/

is for the post post type.

However, I would like to create a new feed, only for the page post type, e.g.

http://www.example.com/kellyspagefeed/

I don't want to override the default one.

Can anyone give me a direction on how to get feed for pages?

0

We can create a custom feed that's accessible from here:

example.tld/kellyspagefeed/

and only shows pages instead of posts.

We could try to rewrite the recent answer here, with the following demo plugin:

<?php
/**
 * Plugin Name: Kelly's Page Feed
 * Description: Accessible from the /kellyspagefeed slug
 * Plugin URI:  https://wordpress.stackexchange.com/a/209244/26350
 */

// Add the custom rss2 feed   
add_action( 'init', 'wpse_init' );

// Set the 'page' post type for the custom feed
add_action( 'pre_get_posts', function( \WP_Query $q )
{
    if( $q->is_feed( 'kellyspagefeed' ) )
        $q->set( 'post_type', 'page' );
} );

// Add our rewrite rules and flush only during plugin activation
register_activation_hook( __FILE__, 'wpse_init' );

// House cleaning during plugin deactivation - remove our rewrite rules
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );

// Helper function
function wpse_init()
{
    add_feed( 'kellyspagefeed', 'do_feed_rss2' );

    if( 'init' !== current_filter() )
        flush_rewrite_rules();  
}

Let's note that this:

register_activation_hook( __FILE__, 'flush_rewrite_rules' );

will not work here, because we have to flush the rewrite rules after we've added our custom feed rewrite rules with add_feed().

So that's why I introduced the wp_init() helper function:

register_activation_hook( __FILE__, 'wpse_init' );

where we make sure the rewrite rules are not flushed within the init hook. That flush is expensive so we only want to do it when our demo plugin is activated or deactivated.

Hopefully you can adjust this to your needs,

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.