Add custom fields to custom post type RSS

I'm looking to add custom fields to a custom post type in the RSS feed. I'm currently using the following to add the custom post type to the RSS feed

function myfeed_request($qv) {
if (isset($qv['feed']) && !isset($qv['post_type']))
$qv['post_type'] = array('post', 'job_listing');
return $qv;
}

Ideally, I'd like for if my custom post type appears in the main feed, then append custom fields, any ideas?

  • Adam Czajczyk

    Hi Dave,

    I hope you're well today and thank you for your question!

    You can both add your custom posts into the main feed and also add values of your custom fields to the same feed. This code should do the trick for you:

    // add custom posts to the main feed
    // keep 'post', post type in array() and replace type1, type2 and so on with your own post types
    function wpmu_custom_post_feed_request($qv) {
        if (isset($qv['feed']) && !isset($qv['post_type']))
            $qv['post_type'] = array('post', 'type1', 'type2');
        return $qv;
    }
    add_filter('request', 'wpmu_custom_post_feed_request');
    
    // include custom fields values
    // adjust Field 1, Field 2 labels and replace field_1, field_2 with your custom fields
    function wpmu_custom_fields_feed($content) {
        if(is_feed()) {
            $post_id = get_the_ID();
            $output = '<div><h3>Additional info:</h3>';
            $output .= '<p><strong>Field 1</strong> ' . get_post_meta($post_id, "field_1", true) . '</p>';
            $output .= '<p><strong>Field 2</strong> ' . get_post_meta($post_id, "field_2", true) . '</p>';
            $output .= '</div>';
            $content = $content.$output;
        }
        return $content;
    }
    add_filter('the_content','wpmu_custom_fields_feed');

    Actually, its first part is pretty much the same that you're already using and it's adding yoru custom posts to the main feed. The second part takes care of custom fields.

    I hope that helps

    Best regards,
    Adam

  • Adam Czajczyk

    Hi Dave!

    I tested this piece of code on my end and it worked fine. This line

    $content = $content.$output;

    actually means that whatever you assign to $output variable in that snippet (in this case some custom text and values of custom fields) are actually added to an original content of the feed, at the end of it.

    Basically, if there are custom feelds added to the post (not even necessarily the custom post) you should see something like:

    Additional info: Field 1: some value Field 2: some value in the feed.

    If those fields doesn't exist you should at list see just "hard coded" text

    Additional info: Field 1: Field 2

    However, one thing to make sure of are the custom fields names (field_1, field_2 in the code) as if there's any error (e.g. typo) in there, they won't be fetched. And another thing is that I tested it on a default setup but there's always a chance that there's some other piece of code (may it be something coming from the theme, may it be some function of another plugin) that's overriding it.

    A possible tweak that might help here (if this is the case) would be to change priority of that "custom fields" snippet so it would be executed later, like this:

    add_filter('the_content','wpmu_custom_fields_feed', 100);

    If for example you have two functions hooking to "the_content" function where the one I shared originally (with no priority explicitly set) has a priority of 10 (default) and the other one has priority set to 15, that other one would be executed later - so it might take over, depending on what it does.

    A value of 100 should be more than enough but you can try even higher one as "the_content" is a popular filter hook widely used by different plugins, so there is a good change that it's being utilized by plugins or a theme on your install in more than one place.

    Best regards,
    Adam

  • Dave

    Thanks again,

    Still no luck. I'm just getting post title, date, time & excerpt. Using the following;

    // add custom posts to the main feed
    // keep 'post', post type in array() and replace type1, type2 and so on with your own post types
    function wpmu_custom_post_feed_request($qv) {
        if (isset($qv['feed']) && !isset($qv['post_type']))
            $qv['post_type'] = array('post', 'job_listing' );
        return $qv;
    }
    add_filter('request', 'wpmu_custom_post_feed_request');
    
    // include custom fields values
    // adjust Field 1, Field 2 labels and replace field_1, field_2 with your custom fields
    function wpmu_custom_fields_feed($content) {
        if(is_feed()) {
            $post_id = get_the_ID();
            $output = '<div><h3>Additional info:</h3>';
            $output .= '<p><strong>Location:</strong> ' . get_post_meta($post_id, "job_location", true) . '</p>';
            $output .= '<p><strong>Salary:</strong> ' . get_post_meta($post_id, "job_salary", true) . '</p>';
            $output .= '</div>';
            $content = $content.$output;
        }
        return $content;
    }
    add_filter('the_content','wpmu_custom_fields_feed', 100);

    Can I enable support access for you to take a look? Any suggestions as to what plugins might cause such an issue?

    Thanks Again

  • Adam Czajczyk

    HI Dave,

    While I did test this code on my end with custom post types and custom fields, there's of course a possibility that some adjustments should be made to it due to e.g. some specific structure/relationships of those types of content on your site or to some interference from some other plugin.

    I would indeed need to check it and I'll be more than happy to take a closer look. However, the support access only gives me a bit limited access. I would most likely need to edit that code on your site and in case anything goes wrong (even a small "typo" like accidentally missing semicolon) it can "cut me off", leaving you with a broken site.

    That said, if you'd like me to give it a go, please provide me with direct access credentials (that will let me safely check the issue and edit code for your site without any risk of not being able to take changes back in case anything failed). To do this please send in:

    Subject: "Attn: Adam Czajczyk"

    - Mark to my attention, the subject line should contain only: ATTN: Adam Czajczyk
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - login URL and admin account login credentials (may be a temporary admin account) data
    - Include FTP log-in details (hostname, username & password)
    - Include hosting control panel access details (login address, username & password) - cPanel's usually the control panel used for this, but your provider may use something else; I'll need this for accessing your site's database, preferably via phpMyAdmin
    - Include any relevant URLs for your site

    Please use our contact form here https://premium.wpmudev.org/contact/#i-have-a-different-question

    Best regards,
    Adam

  • Adam Czajczyk

    Hello David,

    I have received your e-mail and checked your site. I have then conducted some further testing on my test setup and I found out that the custom fields in WP Job Manager have use a slightly different naming convention: they're prefixed with _ (underscore).

    Therefore, to make it work, you'll need to adjust the code a bit. Take a look at these lines:

    $output .= '<p><strong>Location:</strong> ' . get_post_meta($post_id, "job_location", true) . '</p>'; $output .= '<p><strong>Salary:</strong> ' . get_post_meta($post_id, "job_salary", true) . '</p>';

    This is where you add your custom fields and instead of "job_location" and "job_salary" you'll need to use "_job_location" and "_job_salary" accordingly, like this:

    $output .= '<p><strong>Location:</strong> ' . get_post_meta($post_id, "_job_location", true) . '</p>';
     $output .= '<p><strong>Salary:</strong> ' . get_post_meta($post_id, "_job_salary", true) . '</p>';

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Dave,

    Thanks for letting me know about it.

    I'm afraid I'm not able to go further with this working on my setup only. My recent response was based on the tests that I run using configuration similar to yours (that's all I could check using access that I had to the site) but there's still a chance that there's something different "under the hood" or some additional code (e.g. from theme or other plugins or even a different version of the job manager plugin) interfering.

    I got your message regarding server access but I'm not able to provide you with an exact time where I could access the site other than I can do it within around 24 hours since receiving the message from you so if that'd be acceptable, please provide me with FTP access (or cPanel, whichever suits your better) via direct message (just like you did previously: https://premium.wpmudev.org/forums/topic/add-custom-fields-to-custom-post-type-rss#post-1281130) and I'll be happy to check that on your end.

    I'm sure we're on a right path here and it's just something "specific" that I need to find but I would need to do it on your site directly.

    Kind regards,
    Adam