• I have made a custom RSS feed.

    <?php
    
    $postCount = 6;
    $posts = query_posts(
        'showposts='.$postCount.
        '&category_name= mail'
    );
    
    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; ?>
                <pubDate><?php echo mysql2date('D, d M Y H:i:s', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
            </item>
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>  
    </channel>
    </rss>

    and i want the last 7 days of posts with category mail.

    i have tryed this one and it works on site.com/feed/ but not on site.com/feed/admin/

    function wpse334869_filter_feed( $query ) {
        if( $query->is_feed('admin') ) {
            $query->set( 'date_query', array(
                array(
                    'after' => '7 days ago'
                )
            ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse334869_filter_feed' );

    How can i do this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Have you added “admin” as a new feed type?
    https://developer.www.ads-software.com/reference/functions/add_feed/

    You’d normally get a category feed by including it in the request:
    example.com/category/foo/feed/
    But you can force a category in “pre_get_posts” by setting the “category_name” query var.

    Thread Starter webexs

    (@webexs)

    @bcworkz i have added the feed and i can see it but this code dosent work on my own mysite.com/feed/admin but it works on mysite.com/feed . I want too see the posts for the last 7 days in my feed/admin

    function wpse334869_filter_feed( $query ) {
        if( $query->is_feed('admin') ) {
            $query->set( 'date_query', array(
                array(
                    'after' => '7 days ago'
                )
            ) );
        }
    }
    add_action( 'pre_get_posts', 'wpse334869_filter_feed' );
    Moderator bcworkz

    (@bcworkz)

    Have you done anything to allow WP to recognize /feed/admin/ requests? Like adding a rewrite rule to match such a request, then adding a ?feed=admin query string to the rewritten URL.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘RSS feed date’ is closed to new replies.