In the archive page for a custom post type I have this query:

$the_query = new WP_Query( array( 'category_name' => 'Bordeaux' ) );

Then in the loop I have:

if ($the_query->have_posts() );

Et cetera.

The loop correctly searches my posts and displays all posts with the category 'Bordeaux' on the page.

I would like to alter this query so that instead of searching for the text string 'Bordeaux', it will query the title of the page and search for a category with that name.

This will allow me to have just one .php file for all custom post types, rather than individual ones.

I tried the following but it didn't work:

$the_query = new WP_Query( array( 'category_name' => echo the_title(); );

Any help much appreciated!

1 Answer 1


you don't need to echo the title out.

Try this:

$the_query = new WP_Query( array( 'category_name' => get_the_title() );
  • Thanks - I tried this but it leads to a "parse error: syntax error, unexpected ';'" and breaks the theme. What title does get_the_title get?
    – Chris L
    Oct 15, 2017 at 12:00
  • No wait, it worked! There was just a right bracket missing: $the_query = new WP_Query( array( 'category_name' => get_the_title() ) );
    – Chris L
    Oct 15, 2017 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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