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. Add default meta data to a page/post template
  9. How to use Class in Java?
  10. Get name of the current template file
  11. How do you find out which template page is serving the current page?
  12. How to change the default registration email ? (plugin and/or non-plugin)
  13. How do I get the size of an attachment file?
  14. Is there any way to use get_template_part() with folders?
  15. Adding Page Attributes Metabox and Page Templates to the Posts Edit Page?
  16. How can i change email template for new user
  17. Any advantage to using get_header() over include(‘header.php’)?
  18. Enable page templates. How?
  19. Is it possible to override the result of get_template_part()?
  20. Use [embed] filter in template files
  21. What’s the purpose of the paged.php file?
  22. How do you access the Product Short Description in a WooCommerce email template? [closed]
  23. Prevent comments_template() to load comments.php
  24. Is there a way to list all used/unused WP templates?
  25. Page editor missing Templates drop down
  26. Print WordPress template filename(s) for debugging
  27. How to force TinyMCE in WordPress to replace newlines with tags and not with  
  28. How to find the posts page (home page) programatically
  29. WooCommerce: Change template for single product page
  30. View WordPress page template usage (or unused)
  31. How to edit contents of dynamic_sidebar()?
  32. How to check if a shortcode exists?
  33. Where is get_header(‘shop’) file in Woocommerce templates
  34. Custom templates folder
  35. What is the link to my default “archive” page?
  36. How to get slug of current category in taxonomy template?
  37. How to show a under construction page for a domain but still be able to work on index.php?
  38. Change the name of the ‘Default Template’
  39. Customizing get_the_excerpt() to specific length and “Read More” output.
  40. How to Rename a Template File?
  41. get page templates
  42. Echoing Elementor page content in template, but it doesn’t get styles and some widgets are missing
  43. Using WordPress templating for HTML emails
  44. Loading partial templates with AJAX/PJAX
  45. WP frontend output of custom textarea fields not respecting line breaks. In admin it’s OK
  46. How do you check if a WordPress template file exist?
  47. Load entire NextGEN gallery from single thumbnail?
  48. I put my blog on a subpage, how do I get page title?
  49. A special single page templates for posts under a category and all its child category
  50. Custom reset password template after forgot password request
  51. Outputting Canonical Resource URLs Across a Multisite Network?
  52. Gravity Forms Custom Templates [closed]
  53. Child Theme: how to override variables?
  54. How do I get the URL of a specific size featured image?
  55. Where to store some per-template preferences?
  56. add/apend templates ‘transparently’ via plugin to currently active theme or child theme?
  57. stop blog page from using index.php?
  58. How can I modify the default reset (lost) password email text?
  59. How to create tag.php
  60. Template files missing after moving site
  61. How do I override template-tags.php in twentyseventeen theme
  62. Get template part vs locate template function
  63. Programmatically change post templates?
  64. How to hide/redirect the author page
  65. Tools for Converting an Existing Website Design to a WordPress Template?
  66. Gutenberg & Pre-formatted Templates: Core Block Attributes
  67. Restrict a search to a custom post type
  68. passing argument to get_template_part() or a better way to code
  69. Gutenberg Editor – Template Doesn’t Match Error
  70. author.php added to template not working on Nginx server
  71. Test if page is child and has children, if so echo child pages also on grandchild pages
  72. Need to give custom page template for each language in WPML
  73. roots child theme can’t override header.php
  74. How did you incorporate WooCommerce in your own WordPress theme?
  75. Give extra post-meta to RSS feeds
  76. Correct process for a new Page Template?
  77. Templates & CSS – Proper Programming Practice?
  78. How to assign a class to a page with a custom template?
  79. wordpress – load a template based on the URI
  80. When calling wp_title(), do you have to create some kind of “title.php” file?
  81. A shared custom taxonomy between two custom post types
  82. Creating a custom category page with pagination
  83. Create Page that uses specific template
  84. TwentyTen: Overloading template.php files vs. get_template_part
  85. Post formats template
  86. Sharing templates with the JSON API?
  87. New Template — copy existing template and change code? [duplicate]
  88. index.php template is used instead of blog page
  89. Using Includes in Templates in Document Head
  90. Proper Javascript Implementation
  91. How to show scaled featured image in template?
  92. Stream Video Player does not work with do_shortcode()?
  93. Same template for all the terms of a taxonomy
  94. Guest author’s post not display in author template
  95. Modifying searchform.php and search.php to have two kinds of searches
  96. Prevent private post 404
  97. Grid layout “last” class to every third item
  98. Where is the /blog/ url set from?
  99. Create a WordPress template without navigation and footer
  100. Load template inside a parent template
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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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