Advertise Here

Methods to upload custom post types to WP RSS feed

by | Aug 20, 2024 | Etcetera, wordpress maintenance, wordpress seo | 0 comments


Using the default main RSS feed of your wordpress web page, the easiest is to host the same old posts. wordpress creates other feeds for custom post sorts, but what about those posts for your number one feed? In this tutorial, I will show you the code needed to include custom post sorts in the main RSS feed in wordpress.

Let’s dive right into the code. We simply want to hook into the request filter that filters out query variables that can also be passed to the default SQL query number one for a given web page in wordpress. We will want to make sure that we are focused on the RSS feed and that the post_type variable is not already defined.

The code to upload a convention post to your RSS feed looks like this:

/**
 * Control the RSS feed post sorts.
 *
 * @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
 */
add_filter( 'request', function( $query_vars ) {
    if ( isset( $query_vars['feed'] ) && ! isset( $query_vars['post_type'] ) ) {
        $query_vars['post_type'] = [ 'post', 'YOUR_CUSTOM_POST_TYPE_NAME' ];
    }
    return $query_vars;
} );

Remember to keep an eye on where it says YOUR_CUSTOM_POST_TYPE_NAME accordingly. Also, realize that what we are doing is surrounding the value of the post_type variable, so we want to return an array of all the post sorts we want to embed.

RSS Caching Essential Notice: In case you add the code snippet to your web page and talk to your RSS feed, you will not see the custom posts embedded. wordpress caches your RSS feed to quickly lock it. To clear the cache, you will have the option to create a new post or save any provided post.

And while you wanted to include all the custom post sorts for your RSS feed instead of returning an array of post sorts, you will have the option to use the core get_post_types() function. You will be able to see a sample snippet below:

/**
 * Include all custom post sorts in the main RSS feed.
 *
 * @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
 */
add_filter( 'request', function( $query_vars ) {
    if ( isset( $query_vars['feed'] ) && ! isset( $query_vars['post_type'] ) ) {
        $query_vars['post_type'] = get_post_types( [ 'public' => true ], 'names' );
    }
    return $query_vars;
} );

Should You Include Custom Posts in Your RSS Feed Number One?

Whether or not you should include custom post types for your number one RSS feed is actually decided by your webpage. In the General Theme Documentation webpage, all documentation articles are added under a custom post type “medical sciences”. And I don’t add any standard posts to the webpage. So in this example the RSS feed might be empty by default. So, I’m going to use a little code to set the RSS feed to use the medical sciences post type.

I think at best you would keep your custom posts unbiased from the main feed. Posts very similar to portfolios, workforce, testimonials, and products don’t make sense to add to an RSS feed. I honestly can’t think of many instances where you would need to include custom posts in the main feed.

On the other hand, if you want to include them, it’s easy!

In case you have decided to include a convention type post in your number one RSS feed, it is very likely that you will want to remove the post specific feed. In my opinion, I use Yoast search engine marketing on all my websites and use their settings to the max that remove all RSS feeds except the main one like this:

If you are looking for the code to remove an RSS feed of a selected post type, it is a bit tricky because we want to solve a couple of problems.

Disable permalink feed development for post type

If you are registering your own custom post type, then you will definitely have the option to simply modify the arguments to the register_post_type function and set the rewrite feed argument to false. Alternatively, you will have the option to hook into the register_post_type_args filter to modify the post type arguments like this:

/**
 * Remove rss feed permalink building for a convention post kind.
 *
 * @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
 */
add_filter( 'register_post_type_args', function( $args, $post_type ) {
	if ( 'YOUR_CUSTOM_POST_TYPE_NAME' === $post_type ) {
		if ( ! isset( $args['rewrite'] ) ) {
			$args['rewrite'] = [];
		}
		$args['rewrite']['feeds'] = false;
	}
	return $args;
}, 10, 2 );

Using the disabling of permalink creation for post type RSS feeds, when you transfer to your-site.com/post-type/feed/ the URL will return a 404. You want to transfer the transfer to Settings > Permalink and save your settings again to clear your permalinks along with this code.

Simply delaying the creation of the feed permalink does not actually disable the custom post type feed. wordpress will instead create a link to the feed via the structure: your-site.com/post-type?feed=rss2. So you will want to remove those links for search engine marketing purposes. To remove the meta tags from the header of the web page we will use code like this:

/**
 * Remove meta links to a convention post kind RSS feed.
 *
 * @link https://www.wpexplorer.com/how-to-add-custom-post-types-to-rss-feed/
 */
add_action( 'wp', function() {
	if ( is_post_type_archive( 'YOUR_CUSTOM_POST_TYPE_NAME' ) ) {
		remove_action('wp_head', 'feed_links_extra', 3 );
	}
} );

This code will remove the custom post type RSS feed from the archive. Finally the other is to set the has_archive topic when you post your post type falseWhat you would do at most more often than not would be to be able to use a static web page for your custom post type and have more control over the construction.

The post Methods to upload custom post types to WP RSS feed appeared first on WPExplorer.

WP Support Plans

[ continue ]

wordpress Maintenance Plans | wordpress hosting

Learn more



Source link

thatguy
Author: thatguy

Places

Services

  • No Categories

Classifieds

  • No Categories

Events

News

Shopping