First things first, you have a syntax error:
$page_title = the_title();
The the_title()
template tag echoes, rather than returns, the post title. You will want to use get_the_title()
instead:
$page_title = get_the_title();
Second, you can simplify things considerably, simply by excluding the current post from your secondary query, by passing the post__not_in
post parameter to your WP_Query()
call.
Change this:
$args = array( "post_type' => 'recipe', 'tag' => $tags );
…to this:
$args = array(
'post_type' => 'recipe',
'tag' => $tags,
'post__not_in' => array( $post->ID )
);
Then, you’ll have no need to test for it inside your secondary query loop.
I would rewrite your secondary query loop like so:
$related_posts_args = array(
'post_type' => 'recipe',
'tag' => $tags,
'post__not_in' => array( $post->ID )
);
$related_posts = new WP_Query( $related_posts_args );
if ( $related_posts->have_posts() ) :
?>
<ul>
<?php
while ( $related_posts->have_posts() ) : $related_posts->the_post();
?>
<li><a href="https://wordpress.stackexchange.com/questions/76889/<? the_permalink(); ?>"><? the_title(); ?></a></li>
<?php
endwhile;
?>
</ul>
<?php
endif;
wp_reset_postdata();