Support » Themes and Templates » Query posts by title?

  • I.d like to be able to query posts from a custom post type by their title, to list posts that share the same string in the title as the page I’m on. Something like this:

    <?php $thetitle = the_title(); ?>
    <?php query_posts("post_type=myposttype&post_title=$thetitle"); ?>
    <?php while (have_posts()): the_post(); ?>

    …but obviously the ‘post_title’ parameter doesn’t exist. Is there another way to do this? I’m a bit of a novice when it comes to PHP.


Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator t-p


    Am I missing something here? I had already checked that page in the codex and couldn’t see a parameter that I could leverage to query post titles.

    This is the loop I have nested on the page to call in posts from another post type, but at the moment it’s missing an extra parameter to restrict it to posts that share the same string in the title as the current page:

    <?php $track_query = new WP_Query( "post_type=tracks" );
       if ( $track_query->have_posts() ) { ?>
           	   while ( $track_query->have_posts() ) {
               // post stuff here
               } ?>
    <?php } wp_reset_postdata(); ?>
    Moderator t-p


    I can,t tell what’s missing. Sorry.

    Well the first line is only using the post_type parameter. I need it to use this AND also constrain the results to a specific title string. E.g.

    $thetitle = the_title();
    $track_query = new WP_Query( "post_type=tracks&post_title=$thetitle" );

    The thing is, ‘post_title’ isn’t a valid parameter. I was wondering if there’s a clever way around this?

    Instead of $thetitle = the_title();


    $thetitle = get_the_title();

    the_title() has a permalink embedded inside of it, where as get_the_title() is just a string.

    Thanks for the tip JackdUpFord, that’s useful. Unfortunately I still dont have a way to plug it into the query…

    I also need to query by the post title velocity. Have you found any answers yet?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query posts by title?’ is closed to new replies.