Support » Developing with WordPress » How to create a custom rss feed in wordpress?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Can someone help me?

    Maybe you should start by stating here EXACTLY what steps you’ve taken already, and what EXACTLY happened or didn’t happen: what specific result or error message, if any, did you get? (“Without success” is not that useful in this regard.)

    That example feed you’ve linked to seems quite odd to me. It didn’t validate either but I’m thinking that possibly the validator choked on that stunted Media RSS format.

    What results are you after? What is it you need your feed to show?

    Anyway, this plugin may generate the feed you are looking for…

    If the feed you generate with the above plugin won’t validate then try it in whatever APP needs it anyway or pass it through Heiko Behrens’ Feed Validator then try it.

    Thread Starter delaitec



    I managed to solve it. Option 02 is correct.

    The template name must start with “rss-” followed by “feedname.php”, but when calling it just use “feedname” in the url: “”.

    I had given a name “rss-cpx-feed” and now I changed it to “rss-cpxfeed.php” and it worked.

    The dash “-” in the feed name was getting in the way somehow.

    Thanks to everyone who helped.

    Thread Starter delaitec



    There was another question that would like help.

    In option 2, which I am using, the code used, “see below”:
    $postCount = 5; // The number of posts to show in the feed
    $posts = query_posts('showposts=' . $postCount);

    Only defines the number of posts to be displayed.

    I wish I could filter by categories, adding 1 or more. Or excluding a category.

    Moderator bcworkz


    You can pass any argument to query_posts() that’s valid for WP_Query. Find the Category Parameters item in the content table after the jump:
    (deep linking straight to the section doesn’t work right on this page due to hidden code snippets messing up scroll positioning)

    You’ll possibly need to switch to array type arguments instead of string type like you currently use. You cannot mix types and some category arguments require the array type. For example, the array type for your current code:
    $posts = query_posts(['posts_per_page' => $postCount,]);

    FYI, using query_posts() is discouraged because it discards the query already done and makes a new query. It’s preferred that we alter the main default query through the “pre_get_posts” action. However, this approach is very different than passing desired arguments. If you just want something that works, it may not be something you want to get into. But if you’re interested in doing things properly in the most efficient way, it’s worth learning about “pre_get_posts”, it’s commonly used to alter all manner of post queries.

    Thread Starter delaitec


    Hello. @bcworkz

    Thanks for taking the time to help me.

    Before reading your answer, I had found this material:

    That gave me the information I needed to filter my Personalized Feed by categories.

    And in this link it is also informed that it is not recommended to use query_posts and that if it is used, I should use wp_reset_query() right after the loop

    I would really like to do the right thing, but I don’t know where to start or whether it’s viable.

    I will explain here my need, the solution I found, and if you can give me an opinion on how to do this the way I would like to thank:

    I would like to create a Custom Feed, in the parameters that I need to be read by an Indoor Media system.
    So I created this Custom Feed using the following steps:

    01) I added the code below in the functions file of my child theme:

    function customRSS(){
            add_feed('myfeed', 'customRSSFunc');
    add_action('init', 'customRSS');
    function customRSSFunc(){
            get_template_part('rss', 'myfeed');

    02) In the folder of my child theme I created a file called:

    03) Inside the rss-myfeed.php file I inserted the code below that will filter the last 6 posts from the worldnews category.

    Already adding the code wp_reset_query() as recommended.

    $postCount = 6;
    $posts = query_posts(
        '&category_name= worldnews'
    header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
    echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
    <rss version="2.0">
        <title><?php bloginfo_rss('name'); ?> - Feed</title>
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <?php while(have_posts()) : the_post(); ?>
                <title><?php the_title_rss(); ?></title>
                <guid isPermaLink="true"><?php the_guid(); ?></guid>
                <?php if (has_post_thumbnail( $post->ID ) ): ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
                    <enclosure url="<?php echo $image[0]; ?>" type="image/jpeg" />
                <?php endif; ?>
        <?php endwhile; ?>
        <?php wp_reset_query(); / * Resetting the query * / ?>  

    04) This way, when accessing the address: I see my personalized feed, exactly the way I need it.

    05) I configured the Indoor Media system to access every 1 hour to check for new posts.

    Do you believe that this implementation has any serious flaws? or that I can improve?

    And if so, how could I do this implementation in the way you indicated so that I would have the same result, that is, display a custom feed like this?

    Moderator bcworkz


    The only real issue with query_posts() is it’s not very efficient. Functionally it works fine. It’s not “wrong” per se, more like “not ideal”. Ideally, you’d use “pre_get_posts” action to alter the initial query. Similar to the examples on the linked docs page, you’d first check if the query’s is_feed() method returns true. I think you can pass 'myfeed' to that method to specifically check for your feed and exclude other feeds. The point is to identify your query from all other queries that pass through the same action.

    Once identified, set the “posts_per_page” and “category_name” query vars to your preferred values. The query object is passed by reference, so calling its methods operates directly on the actual query object, it’s not a copy which we would normally see passed to our callbacks.

    Thread Starter delaitec


    Thanks again for your willingness to help me.

    I’m not an expert at this, so I’m going to ask ok?

    Should I do this new form of implementation with pre_get_posts inside the rss-myfeed.php file?

    In place of the old query_posts() Code?

    Moderator bcworkz


    My bad for not explaining adequately. A “pre_get_posts” action hook can go in functions.php of your theme, preferably a child theme. Or it can go into any custom plugin you might have active. The best place is anywhere that gets evaluated on every request but is not subject to periodic updates.

    It wouldn’t work on your feed template because by the time evaluation gets there the main default feed query has already been made. You do modify your feed template though because it can now use the default feed query as modified by the “pre_get_posts” callback. There’s no need to re-do the query with query_posts() any more. You may remove the $postCount line and query_posts() code from your template.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.