Support » Developing with WordPress » How to filter FEEDS RSS by category?

  • How to filter FEEDS RSS by category?

    Hello, I used the instructions on this site:
    https://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/

    To create a custom RSS FEED, Works Fine.
    But there is a part of the code that I need help to modify, “see below”:

    $postCount = 5; // The number of posts to show in the feed
    $posts = query_posts('showposts=' . $postCount);

    This code only defines the number of posts to be displayed.

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

    Can someone Help me?

    • This topic was modified 1 month, 4 weeks ago by delaitec.
    • This topic was modified 1 month, 4 weeks ago by delaitec.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Each category has its own feed: https://example.com/category/category-name-here/feed/

    Thread Starter delaitec

    (@delaitec)

    Hello @sterndata

    Thanks for your help, this form you indicated I know, is the native wordpress feed.

    What I would like to do is create a personalized feed, only with the desired items.

    And I did it using query_posts()

    But I also read that it is not recommended to do it using query_posts()

    I will explain below how my implementation is, and if possible I would like your opinion.

    I had found this material:
    https://codex.wordpress.org/pt-br:Refer%C3%AAncia_de_Fun%C3%A7%C3%A3o/query_posts

    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:

    /* ADJUSTING FEED RSS */
    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:
    rss-myfeed.php

    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.

    <?php
    
    $postCount = 6;
    $posts = query_posts(
        'showposts='.$postCount.
        '&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">
    <channel>
        <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(); ?>
            <item>
                <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; ?>
            </item>
        <?php endwhile; ?>
        <?php wp_reset_query(); / * Resetting the query * / ?>  
    </channel>
    </rss>

    04) This way, when accessing the address: http://www.site.com/feed/myfeed I see my personalized feed, exactly the way I need it.

    05) I configured the Indoor Media system to access http://www.site.com/feed/myfeed 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?

    @delaitec very promising. I am trying to do the exact thing you are.

    I copied and pasted the code to functions.php and rss-myfeed.php (removed the worldnews filter).

    But I get a 404 error. The feed is not rendering after clearing my cache and restarting wp. I am navigating to /feed/myfeed

    EDIT: I solved it by going to Settings > permalinks > Save Changes as per this article:
    https://kb.wprssaggregator.com/article/219-how-to-create-custom-rss-feeds

    • This reply was modified 6 days, 13 hours ago by sscott1.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.