Reputation: 366
I have a custom post type, with page capabilities, to create HTML newsletters via WordPress. I am using hierarchy with child posts to create a magazine type newsletter: parent post is the main post, children are media objects below.
See example: http://blog.utc.edu/today/newsletters/whats-the-latest-for-summer-semester-2014/
I need RSS feed for this CPT, but I want to exclude child posts. Is a simple URL parameter to achieve this? (I know I can create a custom RSS template.)
See example: http://blog.utc.edu/today/feed/?post_type=utcblogs_newsletter&include_children=false
The above URL is "optimistic", I know the parameter is not correct. You can see the child posts are included.
I also add the CPT to the general RSS feed by:
// Add newsletters to general RSS feed
function add_cpt_to_feed( $query ) {
if ( isset($query['feed']) && !isset($query['post_type']) )
$query['post_type'] = array('post', 'utcblogs_newsletter');
return $query;
}
add_filter( 'request', 'add_cpt_to_feed' );
So, is there a way to modify the $query to exclude child posts?, or do I need a different query or WP function to achieve this?
Upvotes: 0
Views: 239
Reputation: 366
I solved this by creating a custom RSS template for the Custom Post Type.
To remove child posts (yes, posts can have hierarchy in a CPT), use post_parent
:
$args = array(
'post_type' => 'utcblogs_newsletter',
'post_parent' => 0
);
query_posts( $args );
So, the complete custom RSS template (newsletter-customfeed.php in main theme folder) is:
<?php
/*
Template Name: Custom Newsletter Feed
*/
$numposts = 5;
function custom_rss_date( $timestamp = null ) {
$timestamp = ($timestamp==null) ? time() : $timestamp;
echo date(DATE_RSS, $timestamp);
}
function custom_rss_text_limit($string, $length, $replacer = '…') {
$string = strip_tags($string);
if(strlen($string) > $length)
return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
return $string;
}
$args = array(
'post_type' => 'utcblogs_newsletter',
'post_parent' => 0
);
query_posts( $args );
$lastpost = $numposts - 1;
header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action('rss2_ns'); ?>
>
<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss("description") ?></description>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
<?php do_action('rss2_head'); ?>
<?php while( have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss() ?></title>
<link><?php the_permalink_rss() ?></link>
<comments><?php comments_link_feed(); ?></comments>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><?php the_author() ?></dc:creator>
<?php the_category_rss('rss2') ?>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php get_the_excerpt(); ?>]]></description>
<?php else : ?>
<description><?php echo '<![CDATA['.custom_rss_text_limit($post->post_content, 256).']]>'; ?></description>
<?php $content = get_the_content_feed('rss2'); ?>
<?php if ( strlen( $content ) > 0 ) : ?>
<content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded>
<?php else : ?>
<content:encoded><![CDATA[<?php the_excerpt(); ?>]]></content:encoded>
<?php endif; ?>
<?php endif; ?>
<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
...and the feed URL is set in functions.php and called as http://my.site.com/newsletter.xml
/* Custom RSS Feed for Newsletter CPT */
function create_my_customfeed_newsletter() {
load_template( get_stylesheet_directory() . '/newsletter-customfeed.php');
}
add_action('do_feed_newsletter', 'create_my_customfeed_newsletter', 10, 1);
// creates /feed/feedname and /feedname.xml for each custom CPT RSS
function custom_feed_rewrite($wp_rewrite) {
$feed_rules = array(
'feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1),
'(.+).xml' => 'index.php?feed='. $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'custom_feed_rewrite');
...and to include newsletters in general RSS:
// Add newsletters to general RSS feed
function add_cpt_to_feed( $query ) {
if ( isset($query['feed']) && !isset($query['post_type']) )
$query['post_type'] = array(
'post',
'utcblogs_newsletter',
'post_parent' => 0
);
return $query;
}
add_filter( 'request', 'add_cpt_to_feed' );
Upvotes: 0