Home › Forums › General Issues › relationship field sort order
Hi,
When you create a relationship field in ACF, how do you then use this sort order to display the related on a page?
This is my code:
<?php
$args = array(
'post_type' => 'client',
'posts_per_page' => '8',
'post_status' => 'publish',
'orderby' => 'rand',
);
query_posts($args);
while ( have_posts() ) : the_post();
$cliente = get_the_title();
$logo_hover = get_field('logo_hover');
$logoclient = get_field('logo');
$id = get_the_ID();
$case = get_posts(array(
'post_type' => 'casestudy',
'meta_query' => array(
array(
'key' => 'cliente', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $case ) ?>
<div class="brand">
<?php
if (count($case) > 0) {
foreach( $case as $cliente2 ):
if (count($case) == 1) {
?>
<a href="<?php echo get_permalink( $cliente2->ID ); ?>">
<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
<img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
</a>
<?php
} else {
?>
<a href="<?php echo get_permalink( $case->ID ); ?>">
<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
<img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
</a>
<?php
}
endforeach;
} else {
?>
<a href="javascript:void(0)" style="cursor:default;">
<img class="img-cliente" src="<?php echo $logoclient['url']; ?>" alt="<?php echo $logoclient['alt']; ?>" />
<img src="<?php echo $logo_hover['url']; ?>" alt="<?php echo $logo_hover['alt']; ?>" />
</a>
<?php
}
?>
</div>
<?php
endwhile;
wp_reset_query();
?>
Try changing these to lines:
if (count($case) > 0) {
foreach( $case as $cliente2 ):
to
if (count($case->posts) > 0) {
foreach( $case->posts as $cliente2 ):
I don’t understand what you’re trying to do here
<a href="<?php echo get_permalink( $case->ID ); ?>">
$case in your code is a WP_Query object, so $case->ID has no value.
To get a better idea of what $case holds you can do this so that you can see everything that’s in it.
echo '<pre>'; print_r($case); echo '</pre>';
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!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 7, 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.