• I’m dealing with two custom post types here: shows and bios.

    On the shows single-post page, I’d like to include two excerpts from posts in the bios section.

    Right now I’m just keeping it simple and only pulling in the_title while I figure out the loop.

    <?php
     global $post;
     $myposts = query_posts('numberposts=50&post_type=bios&name=Scott Amendola');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>

    In the query_posts, as an example I have “Scott Amendola” as the value of the name parameter. Instead of me hardcoding that value, I’d like to to be based on the values of set in custom fields on the shows page.

    I know this won’t work, but this is what I’m getting at:

    <?php
     global $post;
     $myposts = query_posts('numberposts=50&post_type=bios&name=$custom_name_here');
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>

    Where it says $custom_name_here, I need that to be a variable set in the custom fields on the Shows Add New page.

    Any ideas on how to make this happen?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter calrockx

    (@calrockx)

    Btw, outside of the loop I have posted above (which queries the post_type=bios), I have another loop that is able to display the $custom_name_here that I have` in mind:

    <?php if ($show_local = get_post_meta(get_the_ID(), 'show_local', true)) : ?>
    									<?php echo $show_local; ?>
    									<?php endif; ?>

    What makes this complicated is that I’m trying to use one post_type’s custom field as the search parameter value for another post_type.

    Thread Starter calrockx

    (@calrockx)

    Well, how about that. I figured it out.

    I wasn’t sure how to properly format the query function, but after some more digging and experimenting, this worked:

    <?php
     global $post;
     $myposts = query_posts("numberposts=50&post_type=bios&name=".$show_local);
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <!-- display content here -->
    
     <?php endforeach; ?>

    So cool, it is possible to use a variable as a parameter in query_posts. ??

    Putting parameters in an $args array might make the code easier to read:

    global $query_string;
    $args = array(
    	'numberposts' => 50,
    	'post_type'  => 'bios',
    	'name' => $show_local
    );
    $myposts = query_posts($query_string, $args);

    Parameter should be ‘posts_per_page’ instead of ‘numberposts’ when using ‘query_posts’. ‘numberposts’ if using ‘get_posts’.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to use a custom field value as a search parameter value in query_posts’ is closed to new replies.