0

I have a custom post type (Books) that is connected with a second custom post type (People). The Books CPT has an additional author meta box that if checked, it should appear on the additional People CPT.

Yet even if checked and assigned a second author, the Book CPT does not show up on that second People CPT's page. My page's code is below:

<?php
    $author_id = get_the_ID();
    $single_books_query = new WP_Query(array(
        'post_type' => 'post',
        'category_name' => 'books',
        'meta_query' => array(                  
           array(
             'key' => 'ecpt_pub_author',                  
             'value' => $author_id,               
             'type' => 'NUMERIC',                 
             'compare' => '='                 
           ),
          'posts_per_page' => '-1'
    ))); 


    if ( $single_books_query->have_posts() ) : while ($single_books_query->have_posts()) : $single_books_query->the_post(); ?>      
            <a href="<?php the_permalink(); ?>">
                <?php if ( has_post_thumbnail()) {  the_post_thumbnail('directory', array('class'   => "floatleft"));  } ?>
                <h5><?php the_title();?></h5>
                <h6><?php if ( get_post_meta($post->ID, 'ecpt_pub_date', true) ) : echo get_post_meta($post->ID, 'ecpt_pub_date', true);  endif; ?><?php if ( get_post_meta($post->ID, 'ecpt_publisher', true) ) :?>, <?php echo get_post_meta($post->ID, 'ecpt_publisher', true);  endif; ?></h6>
        <p><b>Role:&nbsp;<span style="text-transform:capitalize;"><?php echo get_post_meta($post->ID, 'ecpt_pub_role', true); ?></span>

     //This is the line that isn't working:
        <?php if (get_post_meta($post->ID, 'ecpt_author_cond', true) == 'on') { $faculty_post_id2 = get_post_meta($post->ID, 'ecpt_pub_author2', true); ?><br>
           <?php echo get_the_title($faculty_post_id2); ?>,&nbsp;<?php echo get_post_meta($post->ID, 'ecpt_pub_role2', true); ?>
        <?php } ?>

        </b></p>
        </a>
        <hr>
    <?php endwhile; endif; ?>

Do I need to establish another loop to query the ecpt_pub_author2?

UPDATE

I added a relation key to the query that shows the second book on the appropriate People if checked. BUT it also defaults to the first People CPT if the conditional is not checked. Why is this? My updated query:

$author_id = get_the_ID();
$single_books_query = new WP_Query(array(
    'post_type' => 'post',
    'category_name' => 'books',
    'meta_query' => array(   
    'relation'=> 'AND',               
       array(
         'key' => 'ecpt_pub_author',                  
         'value' => $author_id,               
         'type' => 'NUMERIC',                 
         'compare' => '='                 
       ),
       array(
        'key'=>'ecpt_pub_author2',
        'value' => $author_id,               
         'type' => 'NUMERIC',                 
         'compare' => '='       
        ),
      'posts_per_page' => '-1'
))); 
1
  • (moved comment to UPDATE above)
    – timmyg
    Nov 3, 2014 at 14:43

1 Answer 1

1

I'm not sure if this is the whole problem but it seems you 'posts_per_page' is sitting inside your 'meta_query' while it should sit in the same level as 'post_type' and 'category_name'...

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.