Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

get_post_metadata() undefined when used in loop in RSS template

To modify an RSS item’s <link> node value, use the the_permalink_rss filter. This is demonstrated in the code below where if a feature post has a value set for _featureurl, we will return the custom value. Otherwise, the default permalink is returned:

/**
 * Filters the permalink to the post for use in feeds.
 *
 * @param string $post_permalink The current post permalink.
 */
add_filter( 'the_permalink_rss', 'wpse_the_permalink_rss', 10, 1 );
function wpse_the_permalink_rss( $post_permalink ) {
    // Bail if this is not a feature.
    if ( 'feature' !== get_query_var( 'post_type') ) {
        return $post_permalink;
    }

    // Get the permalink URL.
    $feature_permalink = get_post_meta( 
            get_the_ID(),
            '_featureurl',
            true 
    );

    // If the the custom URL has been specified return it, otherwise, use default permalink.
    // Note: This is run through esc_url() via the_permalink_rss().
    if ( $feature_permalink  ) {
        return $feature_permalink; 
    } else {
        return $post_permalink;     
    }
}

Example RSS output for the feature post type, which has the following URL:

http://example.com/feed/?post_type=feature

Note that the <link> node under the <item> node contains the URL https://google.com, which is what I set for the _featureurl custom field:

<?xml version="1.0" encoding="UTF-8"?><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/"
    >

<channel>
    <title>Features &#8211; WP Theme Testing</title>
    <atom:link href="http://localhost/wp-theme-testing/feed/?post_type=feature" rel="self" type="application/rss+xml" />
    <link>http://localhost/wp-theme-testing</link>
    <description>I &#60;3 testing themes!</description>
    <lastBuildDate>Sun, 04 Mar 2018 05:46:16 +0000</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <generator>https://wordpress.org/?v=4.9.1</generator>

<image>
    <url>http://localhost/wp-theme-testing/wp-content/uploads/2017/06/cropped-favicon-32x32.png</url>
    <title>Features &#8211; WP Theme Testing</title>
    <link>http://localhost/wp-theme-testing</link>
    <width>32</width>
    <height>32</height>
</image> 
    <item>
        <title>test feature</title>
        <link>https://google.com</link>


        <comments>http://localhost/wp-theme-testing/feartures/test-feature/#respond</comments>
        <pubDate>Sun, 04 Mar 2018 05:41:29 +0000</pubDate>
        <dc:creator><![CDATA[dave]]></dc:creator>

        <guid isPermaLink="false">http://localhost/wp-theme-testing/?post_type=feature&#038;p=3035</guid>
        <description><![CDATA[a test!]]></description>
                <content:encoded><![CDATA[<p>a test!</p>
]]></content:encoded>
            <wfw:commentRss>http://localhost/wp-theme-testing/feartures/test-feature/feed/</wfw:commentRss>
        <slash:comments>0</slash:comments>
        </item>
    </channel>
</rss>

Related Posts:

  1. How to include template according to meta post value
  2. Why do I get the same excerpt for all items in my RSS feed?
  3. Bizarre issue with custom RSS template
  4. Is it possible to access plugin data from a custom feed template?
  5. the_post() in RSS feed
  6. Mysterious template change after database import/export
  7. How can I setup custom templates for a meta-data query
  8. Get name of the current template file
  9. How do you find out which template page is serving the current page?
  10. How do you access the Product Short Description in a WooCommerce email template? [closed]
  11. Prevent comments_template() to load comments.php
  12. Page editor missing Templates drop down
  13. View WordPress page template usage (or unused)
  14. How do I get the URL of a specific size featured image?
  15. Create Page that uses specific template
  16. Sharing templates with the JSON API?
  17. index.php template is used instead of blog page
  18. Prevent private post 404
  19. Where is the /blog/ url set from?
  20. NextGen Gallery template cache [closed]
  21. Adding variable to get_template_part
  22. Set template based on query in URL
  23. Template tags for Buddypress
  24. ACF image field to WordPress featured image
  25. Child theme template file will not load
  26. How to display only an excerpt of the content with custom post types?
  27. get_template_part returns NULL
  28. ACF – Theming Flexible Content
  29. Is there a way to check which template file is being loaded, if it is not a page template file?
  30. Display “large” image size and show caption in attachment page
  31. How to delete read more span on single post view?
  32. Display Password Protected external RSS in wordpress template
  33. how to make a home page for wordpress custom theme
  34. WordPress editable template fields
  35. Need Category RSS Feed that shows all posts
  36. How do I find the directory of a page template?
  37. Is there a way to set different post templates for parent posts and child posts in the same post type?
  38. Why write markup for index.php?
  39. LaTeX for WordPress strips codes in loop
  40. How to remove the Template drop down, but keep Parent and Order
  41. Search results when none found – stay on page they were on
  42. Having trouble with Template hierarchy. I Need to create a set of pages that drill down from states to specific locations
  43. How can I hardcode template selection?
  44. Changing layout programmatically
  45. How to identify which template WordPress’s default search-form retrieves in my theme
  46. Unable to display pagination links on custom category template
  47. How to use same page template if post status other than ‘publish’?
  48. What’s the purpose of $wp_did_header?
  49. Override Dokan Includes File in Child Theme
  50. How to get link to homepage
  51. How is WooCommerce cart.php template supposed to be used?
  52. Two exact templates, sidebar showing in one but not the other
  53. Trying to display short code content in template file with do_shortcode()
  54. How can I modify RSS item titles to be either the title or a custom meta field?
  55. Taxonomy term page going to 404
  56. How to use index.php as a template for archives?
  57. How can I use default CPT templates from sub-folder?
  58. How to customize WooCommerce templates to avoid override upon update [closed]
  59. Creating new content types (Pages, posts, testimonials, tigers, oh my!)
  60. How to set page title tag in custom template for non-Custom Post Type?
  61. Creating separate feeds for custom post types
  62. Page header on specific page is different from the rest [closed]
  63. Can a link in WordPress contain a query string that is picked up as $_POST
  64. How to implement my custom development multiple PHP page work into WordPress?
  65. How can I get all archive-URLs to use the same template?
  66. Index template always follows page_template() template?
  67. How to use a template file to show all post having same metakey
  68. Automate configuration after new/hosted installation
  69. wp_list_authors() returning the wrong member url
  70. Modify just ONE page based on a template
  71. which template does wp_ tag_ cloud point to?
  72. Pagination problem (plugin wp-pagenavi) with a custom post type?
  73. Add an anchor link to a ‘plain’ HTML file, without over-writing the entire site?
  74. Template part including not working
  75. Parent/Child themes – both CSS files loaded
  76. front-page.php includes a placeholder text field. How to get rid of it?
  77. Display a custom 404 page without a redirect
  78. Why wordpress custom template comments shows Undefined index?
  79. Template and URL parameter problem
  80. get_users by role returns all users
  81. Validating Error with submit button
  82. How can I alter a post?
  83. What is the purpose of the $before and $after arguments on the the_title() function?
  84. Why is this array not working?
  85. Remove ” Browse Category : ” from Archive title
  86. When to use content-pagename.php?
  87. Using external file with WP_USE_THEMES set to false causing 404
  88. strange behaviour of template_redirect in IE8
  89. Current URL path variable
  90. MailPress plugin: table inline style tag removed when sending the newsletter
  91. How to install a wordpress website template on an existing website?
  92. Custom Archive with Content for Custom Post Type
  93. Why is my no-results.php template moving my sidebar and footer into the main container?
  94. Given a page ID, how to display entire page (header/content/footer/sidebars) from a plugin hook
  95. Any way to insert text on page from a query results?
  96. Show page title just from the first child-page in template
  97. How to know which page/file WordPress is expecting?
  98. AJAX requests within templates
  99. Page.php vs Single.php
  100. How to duplicate a template in wordpress site editor?
Categories templates Tags post-meta, rss, templates
Add options to the “Screen Options” section on the “Menus” editor
Automatically wrap multiple images in div

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress