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-dTH:i:sZ' ); ?></published>
<content type="html"><![CDATA[<?php the_content(); ?>]]></content>
</entry>
<?php
// End of the Loop
endwhile;
?>
</feed>
<?php
}
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.