I have the following written to retrieve a list of posts that match the title of the page, by their tag. The problem is it does not play nicely with tags that have multiple words (and perhaps special characters too but I have yet to run into that).

$title_post_tag = new WP_Query( array(
'tag' => strtolower( get_the_title() )
) );

while ( $title_post_tag->have_posts() ) : $title_post_tag->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
<?php the_date(); ?></p>
<?php endwhile;

I have read up on needing to sanitize in php but I'm not entirely sure how to use that in this context or if it would even help me at all.

Guidance is greatly appreciated.


I overlooked get_queried_object()->post_name


$title_post_tag = new WP_Query( array(
'tag' => strtolower( get_the_title() )

I changed it to:

'tag' => get_queried_object()->post_name 

So far, it looks like it is working.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.