Home › Forums › General Issues › How to add post per page with relationship
`I’m trying to figure a way how to make posts per page work. By default shows all. I’ve tried a bunch of ways but no luck even with putting a second query inside the foreach loop.
From acf documentation https://www.advancedcustomfields.com/resources/relationship/
<?php
$eventposts = new WP_Query( array(
‘post_type’ => ‘artist’, // This is the name of your post type – change this as required,
‘posts_per_page’ => 4,
‘order’ => ‘DESC’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘location’, // taxonomy name
‘field’ => ‘term_id’, // term_id, slug or name
‘terms’ => 836, // term id, term slug or term name
)
)
) );
while ( $eventposts->have_posts() ) : $eventposts->the_post();
// The content you want to loop goes in here:
?>
<?php
$featured_posts = get_field(‘relation’);
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<li>
<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>
<span>A custom field from this post: <?php the_field( ‘field_name’ ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Relationship fields do not have “pages”. There is nothing built into the relationship field that could show a certain number of posts with links to show more related posts. You would need to build something yourself like maybe this https://connekthq.com/plugins/ajax-load-more/examples/advanced-custom-fields/relationship/
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.