WordPress.org

Ready to get started?Download WordPress

Forums

Query posts by title? (8 posts)

  1. velocitykendal
    Member
    Posted 3 years ago #

    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.

    Thanks.

  2. Tara
    Member
    Posted 3 years ago #

  3. velocitykendal
    Member
    Posted 3 years ago #

    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() ) { ?>
            <ul>
            <?php
           	   while ( $track_query->have_posts() ) {
               $track_query->the_post();
               // post stuff here
               } ?>
             </ul>
    <?php } wp_reset_postdata(); ?>
  4. Tara
    Member
    Posted 3 years ago #

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

  5. velocitykendal
    Member
    Posted 3 years ago #

    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?

  6. JackdUpFord
    Member
    Posted 3 years ago #

    Instead of $thetitle = the_title();

    try

    $thetitle = get_the_title();

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

  7. velocitykendal
    Member
    Posted 3 years ago #

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

  8. evandavidpaul
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic