0

I'm using custom post types and listing them in single-[customposttype].php?type=example

<?php if (have_posts()) : while(have_posts()) :  the_post();the_content(); endwhile; endif; ?>

Now I have 2 different custom post type "customtype1" and "customtype2". "customtype1" is top category for "customtype2". When I add a new post on "customtype2" select category from metabox(I have a selectbox which listed "customtype1" posts.) in "customtype1" list. My question is how can I use a query for listing customtype2 posts which belongs top category page(single-customtype1.php)

0

This all depends on what you're actually saving in your post meta. If you're actually saving the ID you could create a secondary query on your single-post_type.php page.

<?php if( have_posts() ) : ?>
    <?php while( have_posts() ) :
            the_post();
            $cpt2_meta = get_post_meta( $post->ID, '_meta_name', true ); // Get out Post Type 2 Meta
    ?>
        <?php the_content(); ?>
    <?php endwhile; ?>
<?php endif; ?>

<?php if( ! empty( $cpt2_meta ) ) :
    $cpt2_posts = new WP_Query( array( 
        'post_type' => 'cpt2_slug',
        'posts_per_page' => -1,
    ) );

    if( $cpt2_posts->have_posts() ) : 
?>
    <?php while( $cpt2_posts->have_posts() ) : $cpt2_posts->the_post(); ?>
        <h1><?php the_title(); ?></h1>
    <?php endwhile;?>
  <?php endif; ?>

<?php endif; ?>

First we get the Post Meta ( category ) then we run a secondary query on that "category". Again, this may change depending on what you're saving as your post meta. For more information you can read into WP_Query.

  • Sorry but I need to be more specific. I have 2 custom post types lyrics and artists. In lyrics adding page I'm listing all artists(from artist custom post type) in select meta box. Now I need to list lyrics for specific artist. For example I want to just show rihanna's lyrics in single-{artistcustom}.php How can I use the query? – trikutin Jan 30 '15 at 12:58
  • OK I found it.. – trikutin Jan 30 '15 at 14:11
0

You can use like this

<?php 
$id = get_the_ID();
$squery = new WP_Query( array( 
    'post_type' => 'lyrics',
    'posts_per_page' => -1,
    'meta_key' => 'your_meta_key',
    'meta_value' => $id,
) );

if( $squery->have_posts() ) : ?>
<?php while( $squery->have_posts() ) : $squery->the_post(); ?>
    <h1><?php the_title(); ?></h1>
<?php endwhile;?>
  • Please add an explanation to your answer: why could that solve the problem? – fuxia Jan 30 '15 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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