0

I'm trying to setup a custom RSS feed for attachments with a meta field. I succeeded in making the feed, however it's now overwriting the actual site content.

add_action( 'init', 'add_custom_feed' );
function add_custom_feed() {
  add_feed( 'photos', 'render_photos_feed');
}
function render_photos_feed() {
  //WP_Query for attachment
  get_template_part( 'rss', 'photos' );
}

I have a poor understanding of where exactly a url is created here. What I'm expecting is example.com/photos/feed or example.com/feed/photos. I'm getting example.com/feed/photos but then also example.com/photos, which is where the Page that displays my photos exists. I can flush permalinks and turn off the action for this feed and render my page, but when it's online, I can see on the Photos page that its permalinked to photos-2. I can't find any documentation online about why that happens.

I really don't want to create a CPT for these, the workflow of adding a checkbox was really easy & reduces noise on the backend and the WP Query works. Secondarily, I also am wondering if it's possible to change the main feed to be posts & photos. I can't just hijack the feed WP_Query because its an OR logic (get posts or attachment w/ meta value). Any thoughts there are welcome too.

1 Answer 1

0

You can use feed/photos with add_feed. This rule will prevent the overwrite permalinks issue. Change your function like below and flush rewrite rules.

add_action( 'init', 'add_custom_feed' );
function add_custom_feed() {
  add_feed( 'feed/photos', 'render_photos_feed');
}
function render_photos_feed() {
  //WP_Query for attachment
  header("Content-type: text/xml"); // Added for XML output
  get_template_part( 'rss', 'photos' );
}
5
  • Great, so that works to get feed/photos as stated. Would you expect that link as standard for syndicating my content?
    – HarrisonFM
    Jun 8, 2021 at 1:57
  • The feed is the standard way to distribute your content. Also, you can use http://www.yoursite.com/feed/?post_type=book&genre=romance like links in WordPress for CPT's or taxonomies. But this way could be easy to remember and use. So, np for me :) Jun 8, 2021 at 18:44
  • Cool, thanks. Would you have any tips for merging my feeds?
    – HarrisonFM
    Jun 8, 2021 at 18:57
  • Into one feed link? You can try this. php function myfeed_request($qv) { if (isset($qv['feed']) && !isset($qv['post_type'])) $qv['post_type'] = array('post', 'story', 'books', 'movies'); return $qv; } add_filter('request', 'myfeed_request'); Jun 9, 2021 at 18:17
  • Oh thanks, but I don't think that works for me. I have a posts feed, and then this feed you helped me with is attachment, with a meta value. So, I need OR logic, posts OR attachments w/ a meta val.
    – HarrisonFM
    Jun 9, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.