Connect post ID with tags?

I have three different posts in a custom post type (services), and when i open one of these in single.php I want to show posts from another custom post type(projects) under the main-post. But I only want to show posts from projects that is related with the post from services.

Like, IF post from “services” has ID 1, show post from “projects” tagged “service1”.


hello there,

You can use this :

<?php $query = new WP_Query( array( "post-type" => "your-post-type", "tag" => "The-TAG" ) ); ?>

For more information about this query :


Thanks for the answer! However, I need to do this dynamically. I need to connect certain posts from a post type with certain posts from another post type. And I don’t know how to do this.

I can show you how the website is built so you can see what I mean.

On the first-page you see three posts/sections named “byggnation”, “fasadrenovering”, “fastighetsfövaltning”, these belong to the same post-type. And if you click “läs mer(read more)” you open single.php and display the whole post. Inside the post you can see “utförda projekt” to the right. This is another post type, that displays different projects for the specific service/post you clicked.
ATM “utförda projekt” displays ALL projects from the three different services that i wrote above. The thing I want to do is to only show the projects that is related to the opened post.

ok, so the code i gave should work for the first possibility : Display posts by Tag.

if you would like to show Specific posts related to a post type so i suggest to use the Post ID instead of tags :

if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Reset Query

where POST-TYPE-NAME is the post type and 1,2,3 is the ID of the posts related to the Post Type you want to display…


Thanks but I don’t think it will solve my problem. Here is how my loops for single.php looks ATM:


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php endwhile; else: ?>

	<p>There are no posts</p>

<?php endif; ?>



$args = array(

	'post_type' => 'projects'


$the_query = new WP_Query( $args );


<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<?php endwhile; else: ?>

	<p>There are no posts</p>

<?php endif; ?>

The first loop gets the single post when the user clicks the “read more” button.

The second loops gets the projects. And this one have to get the right project-posts depending on what post is displayed by the first loop. Like if the first project has ID=1 the second loop displays post with the tag “service1”. And because of this is the single-post.php I can’t really do it your way because that would show the same result regarding what post the visitor opens? right?