Title: display custom post based on title
Last modified: August 31, 2016

---

# display custom post based on title

 *  Resolved [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/)
 * I have a page of athlete results. On that page is Name, Sport, Date and result(
   score). I also have athlete profile pages. Both pages are using Custom Post Types.
   I would like to display results on the athlete page of that athlete. I am able
   to display all results on the athlete page but would like it if only that athletes
   results display.
 * I have been stuck on this for awhile, I am hoping someone can help me out.

Viewing 15 replies - 1 through 15 (of 31 total)

1 [2](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/2/?output_format=md)

 *  [lisa](https://wordpress.org/support/users/contentiskey/)
 * (@contentiskey)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259123)
 * are you comfortable changing php in your theme templates?
    are you using a child
   theme?
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259137)
 * I am good with changing the php. I made the template but am just learning.
 *  [lisa](https://wordpress.org/support/users/contentiskey/)
 * (@contentiskey)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259167)
 * can you share the code for the template that have tried for showing the athlete’s
   single post profile page + athlete’s results display.
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259209)
 * here is what I have so far. This brings in all the results for all athletes. 
   All I want is for the results for that athlete.
 *     ```
       <section class="col-sm-12">
                   <?php
                     $args = array(
                       'post_type' => 'result',
                       'category_name' => 'current_athlete',
                       'orderby' => 'date',
                       'order'   => 'ASC',
                       'posts_per_page'  =>  15,
                       'paged'             =>  $paged,
                       );
                     $the_query = new WP_Query( $args );
                   ?>
                   <?php if (the_field('result-athlete') == $current_title ); ?>
                   <div class="col-md-12 index-results-Section">
                   <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                     <div class="index-resultsSingle">
                        <div class="col-md-2 index-resultsAthlete">
                           <?php the_field( 'result-athlete' ); ?>
                         </div><!-- .index-resultsAthlete -->
   
                         <div class="col-md-7 index-resultsSport">
                          <?php the_field( 'result-sport' ); ?>
                          <?php the_field( 'result-date' ); ?>
                         </div> <!-- .index-resultsSport -->
   
                         <div class="col-md-3 index-resultsResult">
                           <?php the_field( 'result-result' ); ?>
                         </div> <!-- .index-resultsResult -->
   
                         <div class="clearfix"></div>
   
                      </div>
   
                     <?php endwhile; endif; ?>
                   <div class="clearfix visible-sm"></div>
                   </div>
               </section><!-- .athlete main-content -->
       ```
   
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259210)
 * You can set meta_key and meta_value as follows:
 *     ```
       $args = array(
         'post_type'       => 'result',
         'category_name'   => 'current_athlete',
         'orderby'         => 'date',
         'order'           => 'ASC',
         'posts_per_page'  => 15,
         'paged'           => $paged,
         'meta_key'        => 'result-athlete',
         'meta_value'      => $current_title,// should be defined beforehand
       );
       ```
   
 * And delete the if clause:
    `<?php if (the_field('result-athlete') == $current_title);?
   >`
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259225)
 * Thanks, ikaring,
 * As I am still quite new to WP I do have a couple of other questions about this.
 * Where would I have defined $current_title?
    And how would I have defined it.
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259230)
 * It can be anywhere before its been used, like so:
 *     ```
       $current_title = wp_title( '', false );
       $args = array(
         ....,
         meta_value'      => $current_title,
       );
       ```
   
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259237)
 * Thanks ikaring. I hadn’t placed that. I am unable to read up on this at the moment
   but how does this work and what is the ”, false for?
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259273)
 * You can learn tags in Codex. Pls check them.
 * [https://developer.wordpress.org/reference/functions/wp_title/](https://developer.wordpress.org/reference/functions/wp_title/)
 * `wp_title( '', false )` means no separate string and just get the title text 
   instead of displaying it on screen.
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259316)
 * I have fixed all the code and I think it should work. I can pull in all of the
   results, but can’t narrow it down to just the athlete that is on the profile 
   page. Here is the new code.
 *     ```
       <?php
               $athlete  =  wp_title( '', false);
             $args = array(
               'post_type'       =>   'result',
               'orderby'         =>   'date',
               'order'           =>   'ASC',
               'posts_per_page'  =>    15,
               'paged'           =>    $paged,
               'meta_query'      =>    array(
                 array(
                     'meta_key'     =>  'result-athlete',
                     'meta_value'   =>  $athlete,
                     'compare'  =>  '='
                   )
                 )
               );
             $the_query = new WP_Query( $args );
           ?>
   
           <div class="col-md-12 index-results-Section">
   
             <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
   
             <div class="index-resultsSingle">
                <div class="col-md-2 index-resultsAthlete">
                   <?php the_field( 'result-athlete' ); ?>
                 </div><!-- .index-resultsAthlete -->
   
                 <div class="col-md-7 index-resultsSport">
                  <?php the_field( 'result-sport' ); ?>
                  <?php the_field( 'result-date' ); ?>
                 </div> <!-- .index-resultsSport -->
   
                 <div class="col-md-3 index-resultsResult">
                   <?php the_field( 'result-result' ); ?>
                 </div> <!-- .index-resultsResult -->
   
                 <div class="clearfix"></div>
   
              </div>
   
             <?php endwhile; endif; ?>
   
           <div class="clearfix visible-sm"></div>
   
           </div>
       ```
   
 * If anyone can’t see where I am going wrong I would greatly appreciate it.
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259318)
 * It seems fine. Just check to see if $athlete value is exactly same as result-
   athlete field value.
 *     ```
       $athlete  =  wp_title( '', false);
       var_dump( $athlete );
       ```
   
 * Which template file is these code written? Is it single-profile.php or single-
   athlete.php?
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259325)
 * I did a var_dump and it printed out the name of the athlete. But for some reason
   it is still outputting all the results no matter what name it is.
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259328)
 * Ah have you set $paged?
    `$paged = ( get_query_var('paged') ) ? get_query_var('
   paged') : 1;`
 *  Thread Starter [Vince_M](https://wordpress.org/support/users/vince_m/)
 * (@vince_m)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259337)
 * Man I am starting to feel stupid. Where do I put this piece of code? and is there
   any tutorials about all of this?
 *  [ikaring](https://wordpress.org/support/users/ikaring/)
 * (@ikaring)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/#post-7259338)
 * Variables ( like $paged or $athlete ) should be defined before it is used like
   so.
 *     ```
       $athlete  =  wp_title( '', false);
       $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
       $args = array(
       ...
       ```
   
 * As for $paged, it is explained in Codex: Pagination Parameters section of WP_Query
   page.
 * [http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters](http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters)
 * meta_query is also in the same page, Custom Field Parameters section.
    Codex 
   is the best place to learn WordPress.

Viewing 15 replies - 1 through 15 (of 31 total)

1 [2](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/3/?output_format=md)
[→](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/2/?output_format=md)

The topic ‘display custom post based on title’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [look](https://wordpress.org/support/topic-tag/look/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 31 replies
 * 3 participants
 * Last reply from: [ikaring](https://wordpress.org/support/users/ikaring/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/display-custom-post-based-on-title/page/3/#post-7259433)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
