Home Forums Front-end Issues Limit relationship posts per page


Limit relationship posts per page

  • How do you limit the number of posts pulled from a relationship field. I’m able to pull all of the posts but can’t figure out how to limit them.

    I image it would be something like this, but either somethings messed up or I don’t fully understand this code:

      $talks = get_posts(array(
        'post_type'      => 'conferences',
        'posts_per_page' => 3,
        'meta_query'     => array(
            'key'     => 'conference_talks',
            'value'   => '"3864"',
            'compare' => 'LIKE'
      <?php echo var_dump( $talks ); ?>
    array(0) {}

    where the Post is:
    Title: New York Conference 2013
    ID: 3864
    Custom post type: conferences
    conference_talks (ACF Relationship field): Value is a couple other posts from the ‘talks’ custom post type

  • Hi @blg002

    If you wish to get the selected ‘relationship’ posts, just simply use the get_field function like so:

    $posts = get_field('conference_talks');

    The code you have posted is for a reverse lookup, not a simple get value


  • thanks @elliot but I think that just gets me back to my original question of how to limit that query, say to just return 3 posts (bonus points for returning random posts). It seems by default it just returns all the posts in the relationship.

    I’m basically looking for the equivalent of:

    <?php $query = new WP_Query(array(
        'posts_per_page' => 3,
        'orderby'        => 'rand',
  • Hi @blg002

    Normaly, you would use the get_field to return an array of $post object, but it is possible to use the get_field to return only the post ID’s, which you can then use in your WP_Query to perform the limit and orderby params.

    // get only first 3 results
    $ids = get_field('conference_talks', false, false);
    $query = new WP_Query(array(
    	'post_type'      	=> 'conferences',
        'posts_per_page'	=> 3,
    	'post__in'			=> $ids,
    	'post_status'		=> 'any',
        'orderby'        	=> 'rand',

    Note that the get_field function has 2 false parameters. The first param is for the $post_id and is not relevant, but the second one is to tell ACF not to format the value, and return only what is in the DB (array of IDs)

    Hope that helps.


  • I’m trying to accomplish the same thing but also to randomize them. I’ve tried the above method but no matter what I do it shows only one of my custom posts and 45 lines of repeating empty divs. There are only 4 custom posts altogether.

  • Hi @esparks

    The above code includes an 'orderby' => 'rand', parameter which will randomize the returned data.

    Perhaps you need to do some debugging of the data returned and double check your args are correct.


  • Do you have a full example? It works fine using a typical relationship set up.

  • I have the same problem, this is my code, what’s wrong?

    $ids = get_field('correla_blog_post');
    $query = new WP_Query(array(
    	'post_type'      	=> get_post_type(),
        'posts_per_page'	=> 1,
    	'post__in'			=> $ids
    if( $ids ): ?>
        <div class="posts-list clearfix">
        <?php foreach( $ids as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <article class="post-item">
            	<figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('t300x200'); ?></a></figure>
                <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <div class="entry-excerpt"><p><?php the_field('riassunto_per_anteprima'); ?></p></div>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    If I change ids to $posts in the foreach it show me 1 result but without thumbnail and extra-fields.

  • I was never able to find a solution and never heard back from support.

  • Off topic:

    Don’t hijack somebody else’s thread. Go create your own thread topic 😉

  • So I want to do something similar to this and I understand the solution of returning the IDs and using a WP_Query to get only the ones I want. Obviously that creates an extra DB call, so my question is one of performance. Is it better to return the IDs and do the extra WP_Query or would it work to just use PHP to limit the array result after the fact with PHP? Maybe using array_slice For example:

    $posts = get_field('conference_talks');
    $posts= array_slice($posts, 0, 5);
  • I guess it depends on what order you want the posts returned in and what 5 you want to limit it to. By default, I believe, that the order that they’re returned when getting the field is the order that they are in relationship field, although I could be wrong. I just had a look and there isn’t any way to alter the order then getting the field.

    If you return an array of ID values, no query of posts is done before they are returned when using get_field(), so there isn’t any double query happening if you’re not returning post objects.

    You are right though if you return post object and do a second query then you’re basically doing 2 queries to get almost the same thing. This is where it’s important to consider and select what is returned when setting up ACF fields for what you intend to do with it later.

  • Thanks, this information is helpful. So just to be clear the order returned for post objects is the order they are in the relationship field (I assume from top to bottom?). This is what I think WordPress calls “menu_order” is that correct? If that’s so, in my case this will work for my scenario.

  • I think that’s the order, at least that’s what I remember the last time I was concerned about the order they appeared in on a site I built. You may want to test it out to be sure.

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

The topic ‘Limit relationship posts per page’ is closed to new replies.