Home › Forums › General Issues › 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">×</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> </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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.