Content Marketing

How To Combine Posts and Custom Post Types In WordPress Queries and RSS Feed

One of the most amazing features of WordPress is the ability to build Custom Post Types. This flexibility is fantastic… as custom post types can be utilized for a business to organize other types of posts like events, locations, FAQs, portfolio items easily. You can build custom taxonomies, additional metadata fields, and even custom templates to display them.

On our site at DK New Media, we have a custom post type set up for projects in addition to our blog where we’re sharing company news. By having a custom post type, we’re able to align the projects on our capabilities pages… so if you view our WordPress services, the projects that we’ve worked on that are WordPress-related will automatically display. I’m hard at work trying to document all of our projects so that our site visitors can see the array of work we do for companies.

Merging Posts and Custom Post Types

Our home page is already quite extensive, so I didn’t want to have to build a section for our blog posts AND a section for our latest projects. I want to merge both posts and projects into the same output using our template builder, Elementor. Elementor doesn’t have an interface to merge or combine posts and custom post types, but it’s quite simple to do this yourself!

Within your child theme’s functions.php page, here’s an example of how to combine the two:

function add_query_news_projects( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'project' ) );
	return $query;
}
add_filter( 'pre_get_posts', 'add_query_news_projects' );

The pre_get_posts filter enables you to update the query and set it to get both your post and project custom post type. Of course, when you write your code you’ll need to update the custom post type(s) to the actual naming convention of yours.

Merging Posts and Custom Post Types in Your Feed

I also have the site automatically publishing to social media via its feed… so I also wanted to use the same query to set RSS feed. To do this, I just had to add an OR statement and include is_feed.

function add_query_news_projects( $query ) {
	if ( is_home() && $query->is_main_query() || is_feed() )
		$query->set( 'post_type', array( 'post', 'project' ) );
	return $query;
}
add_filter( 'pre_get_posts', 'add_query_news_projects' );

Merging Posts and Custom Post Types in Elementor

One more note… Elementor has a really great feature where you can name and save a query within your site. In this case, I am building an query called news-projects and then I can call it from the Elementor user interface in the Posts Query section.

function my_query_news_projects( $query ) {
	$query->set( 'post_type', array( 'post', 'project' ) );
}
add_action( 'elementor/query/news-projects', 'my_query_news_projects' );

Here’s how it looks in the Elementor user interface:

elementor posts query

Douglas Karr

Douglas Karr is a fractional Chief Marketing Officer specializing in SaaS and AI companies, where he helps scale marketing operations, drive demand generation, and implement AI-powered strategies. He is the founder and publisher of Martech Zone, a leading publication in marketing technology, and a trusted advisor to startups and enterprises… More »
Back to top button
Close

Adblock Detected

We rely on ads and sponsorships to keep Martech Zone free. Please consider disabling your ad blocker—or support us with an affordable, ad-free annual membership ($10 US):

Sign Up For An Annual Membership