Support

Account

Home Forums General Issues Meta query on a CPT not retrieving post object from another CPT

Solving

Meta query on a CPT not retrieving post object from another CPT

  • I have custom post type 1 (‘api’) and custom post type 2 (‘metabolites’). On CPT #2, there is a Post Object custom field (called ‘metabolites_select_api’) to select the associated CPT #1. Thus, all CPT #2 would be called onto the CPT #1 based on the post object selection. Seems straightforward and I have tried the meta_query solution and the get_posts solution, but to no avail. Any guidance or suggestions would be much appreciated. Below is my code.

    <?
    $args1 = array(
    	'post_type' => 'metabolites',
    	'meta_query'      => array(
    	  array(
    		'key'     => 'metabolites_select_api',
    		'value'   => '"' . get_the_ID() . '"',
    		'compare' => 'LIKE'
    	  )
    	)
    );
    $query1 = new WP_Query( $args1 );?>
    
    <table id="rowTable" class="display" cellspacing="0" width="100%">
    	<thead>
    		<tr>
    			<th width="15%">Structure</th>
    			<th>Name</th>
    			<th width="5%">Ref</th>
    			<th width="5%">CAS #</th>
    			<th>Notes</th>
    		</tr>
    	</thead>
    	<tbody>
    
    <?php $id_increment = 0; ?>
    <?php while ( $query1->have_posts() ) : $query1->the_post(); ?>
    	<tr>
    		<td><?php if ( get_field( 'metabolites_structure' ) ): ?><img src="<?php the_field('metabolites_structure'); ?>" alt="structure"><?php endif; ?></td>
    		<td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
    		<td><!-- BUTTON FOR MODAL POPUP BOX -->
    			<button onclick="document.getElementById('id4_<?php echo $id_increment; ?>').style.display='block'" class="w3-button"></button>
    
    			<!-- MODAL POPUP BOX -->
    			<div id="id4_<?php echo $id_increment; ?>" class="w3-modal">
    				<div class="w3-modal-content">
    				  <header class="w3-container w3-teal"> 
    					<span onclick="document.getElementById('id4_<?php echo $id_increment; ?>').style.display='none'" 
    					class="w3-button w3-display-topright">&times;</span>
    					<h2>References</h2>
    					</header>
    
    				<div class="w3-container results">
    				<table width="100%" cellpadding="0" cellspacing="0" border="0">
    					<tr>
    						<td class="header">Title</td>
    						<td class="header">Description</td>
    						<td class="header number">Ref #</td>
    						<td class="header">PDF</td>
    					</tr>      
    
    					<?php 
    
    					// get selected people
    					$metareferences = get_field('metabolites_references');
    
    					if( $metareferences ): ?>
    
    						<?php foreach( $metareferences as $metaref ): ?>
    						<tr>
    							<td><?php echo get_the_title( $metaref->ID ); ?></td>
    							<td><?php echo get_field('reference_description', $metaref->ID); ?></td>
    							<td class="number"><?php echo get_field('reference_number', $metaref->ID); ?></td>
    							<td><?php echo get_field('reference_pdf', $metaref->ID); ?></td>
    						</tr>
    					<?php endforeach; ?>
    				  <?php endif; ?>
    
    				  </table>
    			  </div>
    			  <footer class="w3-container">
    				<p>&nbsp;</p>
    			  </footer>
    		  </div>
    
    		 <?php $id_increment++; ?>
    
    		</div>
    		<!-- end MODAL POPUP BOX -->
    		</td>
    		<td><?php the_field('metabolites_cas'); ?></td>
    		<td><?php the_field('metabolites_notes'); ?></td>
    	</tr>
    <?php endwhile; ?>
    
    	</tbody>
    
    </table>
  • Hi @deanna-saab

    I would suggest that you replace this line:

    'value' => '"' . get_the_ID() . '"',

    with a variable containing the post id selected using the get_field().

    Please note that it is vital to ensure that the return type is set to post id.

  • Thanks James! I will give that a try. Appreciate it!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Meta query on a CPT not retrieving post object from another CPT’ is closed to new replies.