1

I am creating a custom feed for one of my post types, and letting users set the name of that feed.

http://www.mysite.com/feed/userdefinedname

Now I'd like to also enable users to show not only all the posts from that CPT but also filter them by category. At the moment it works like so:

http://www.mysite.com/feed/testcategory

However I'd very much prefer it if I could structure it as follows:

http://www.mysite.com/feed/userdefinedname/testcategory

Is that possible?

Here's the code that is generating these category based feeds:

 /**
 * Generate the feeds for categories
 *
 * @since 1.0
 */
function rss_c_add_category_feed( $in ) {

    $category = $GLOBALS['wp_rewrite']->feeds[ count( $GLOBALS['wp_rewrite']->feeds ) - 1 ];

    // Prepare the post query
    // Get published rss_feed posts with a rss_category slug in the taxonomy
    $rss_custom_feed_query = apply_filters(            
        'rss_custom_feed_query',
        array(
            'post_type'   => 'rss_feed', 
            'post_status' => 'publish',
            'cache_results' => false,   // disable caching
            'tax_query' => array(
                array(
                    'taxonomy' => 'rss_category',
                    'field'    => 'slug',
                    'terms'    => array( $category ),
                    'operator' => 'IN'
                )
            )
        )
    );

    // Submit the query to get latest feed items
    query_posts( $rss_custom_feed_query );

    $sources = array();
    while ( have_posts() ) {
        the_post();
        $sources[] = get_the_ID();
    }


    // Create the query array
    $pre_query = array(
        'post_type'      => 'rss_feed_item', 
        'post_status'    => 'publish',
        'cache_results'  => false,   // disable caching
        'meta_query'     => array(
                                array(
                                    'key'     => 'rss_feed_id',
                                    'value'   => $sources,
                                    'compare' => 'IN'
                                )
        )
    );

    // Get options
    $options = get_option( 'rss_settings_general' );
    if ( $options !== FALSE ) {
        // If options exist, get the limit
        $limit = $options['custom_feed_limit'];
        if ( $limit !== FALSE ) {
            // if limit exists, set the query limit
            $pre_query['posts_per_page'] = $limit;
        }
    }

    // query the posts
    query_posts( $pre_query );

    // Send content header and start ATOM output
    header('Content-Type: application/atom+xml');
    // Disabling caching
    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
    header('Pragma: no-cache'); // HTTP 1.0.
    header('Expires: 0'); // Proxies.
    echo '<?xml version="1.0" encoding="' . get_option('blog_charset') . '"?' . '>';
    ?>
    <feed xmlns="http://www.w3.org/2005/Atom">
        <title type="text">Latest imported feed items on <?php bloginfo_rss('name'); ?></title>
        <?php
        // Start the Loop
        while ( have_posts() ) : the_post();

        ?>
        <entry>
            <title><![CDATA[<?php the_title_rss(); ?>]]></title>
            <link href="<?php the_permalink_rss(); ?>" />
            <published><?php echo get_post_time( 'Y-m-d\TH:i:s\Z' ); ?></published>
            <content type="html"><![CDATA[<?php the_content(); ?>]]></content>
        </entry>
        <?php
        // End of the Loop
        endwhile;
        ?>
    </feed>
    <?php
}
  • 2
    How did you make this mysite.com/feed/testcategory work? WordPress by default adds mysite.com/testcategory/feed links, I guess. Show some of your code. I should be able to help you then. – Krzysiek Dróżdż Jul 29 '13 at 7:31
  • Thanks for posting the code @drtanz but in addition can you give us an example of the URL you would use to access this when not using rewrite then it should be simple to suggest the appropriate rewrite rules you need. – Mark Davidson Jul 31 '13 at 8:29
  • Can you provide this so its possible to help you? – Mark Davidson Aug 1 '13 at 19:58
  • Sure, right now it works if I do mysite.com/feed/mycustomfeedname. Also if I use mysite.com/feed/categoryname. – urok93 Aug 2 '13 at 13:26
0

As Krzysiek says its hard to give a good answer without knowing how your doing your feeds. But assuming that you've extended the default wordpress feeds so your feed could be accessed by going to index.php?feed=userdefinedname, then this rewrite rule should do the job for you.

add_rewrite_rule(
    '^feed/(.+)/(.+)?/?$',
    'index.php?feed=$matches[1]&category_name=$matches[2]',
    'top'
); // /feed/userdefinedname/testcategory

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.