Support

Account

Home Forums General Issues How to add post per page with relationship

Helping

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/

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

You must be logged in to reply to this topic.