Support

Account

Home Forums General Issues Next post in relationship field

Solving

Next post in relationship field

  • Hi,

    I’m trying to solve a problem. I’m using a relationship field to select the projects I want to show on a website. When in the single page of one of these projects, I want to be able to navigate to the next or previous post of the selected projects.

    My idea is to query the relationship field, and output it as an array. And then select the two projects around a certain post id. But what if this post id is in the end of begin of the array.

    I can’t figure out this problem, so I hope someone can help me in the right direction.

    Thanks in advance

  • You ever figure this out? I’m in the same boat.

  • I’m very curious about this as well. Any way to do this?

  • Ok, I eventually solved it, but I must admit it isn’t the cleanest way of doing it. I followed the idea to put all items in an array, and select the one before (and after) the current position.

    First I retrieved the list of projects to show.

    
    $posts = get_field('projectSelector', 42); // include id of page to get it to work propperly
    

    Then create two functions:

    
    function prev_link($posts, $current) {
        for ($i = 0; $i < count($posts); $i++) {
            if ($current->ID == $posts[$i]->ID) {
                if (isset($posts[$i-1])) {
                    return $posts[$i-1];
                }
            }
        }
        return $posts[count($posts)-1];
    }
     
    function next_link($posts, $current) {
        for ($i = 0; $i < count($posts); $i++) {
            if ($current->ID == $posts[$i]->ID) {
                if (isset($posts[$i+1])) {
                    return $posts[$i+1];
                }
            }
        }
        return reset($posts);
    }
    

    Next step is to create the links:

    
    if ($posts) {
        $post = prev_link($posts, $post);
        setup_postdata($post);
    ?>
        <a href="<?php echo get_permalink($post->ID); ?>" class="prev-post-button"><?php e_i18n("Vorig project", "Previous project"); ?></a>
    <?php
        wp_reset_postdata();
    }
     
        $post = next_link($posts, $post);
        setup_postdata($post);
    ?>
         
        <a href="<?php echo get_permalink($post->ID); ?>" class="next-post-button"><?php e_i18n("Volgend project", "Next project"); ?></a>
     
    <?php
     
        wp_reset_postdata();
     

    Setting up the post data is necessary to obtain the right post ID’s.

    Notice that the prev link picks the previous from the current, and the next the next from the current. I’m also checking where in the array I am, so that when I am at the start or the end, it returns the last or the first from the array.

    Hope this will help you guys with this issue. I got some help from @thijsw

  • For anyone still looking for help with this, I wrote an article/tutorial on how to build Previous and Next post navigation around the Relationship field.

    You can check it out below:
    https://thatwebdude.com/tutorial/how-to-create-previous-and-next-posts-with-advanced-custom-fields/

    This builds around what @sytheveenje was trying to accomplish, but with a little more structure and organization.

    I hope it’s helpful!

  • I’m running into the same problem but @ali_jafarian solution isn’t quite working perfectly for me. I have a CPT for artworks with custom taxonomy for artists. Then I have another CPT for artists. The artist pages are basically the the page title (artist name) and a relationship field where you add the artist’s artwork from the artworks CPT where you can also filter by the custom taxonomy (since there are 50+ artists and 100s of artworks).

    Each artwork is its own page as mentioned. When you click on the artwork from an artist page, we want to have the option of going to the next or previous artwork based on the order displayed on the artist page as determined by the order you set from its relationship field.

    There seems to be an issue with the following code. I’m note sure how $current_post_id applies to the array_search as it isn’t referencing anything anywhere else in the code. If I print_r $current_index nothing appears. Same goes for $prev_module and $next_module. However, if I print_r $module_ids the array appears as it should in the order I set on the Artist page in the Relationship field. $first_module and $last_module show the correct IDs too. I just can’t seem to get the current post ID to then determine next and previous.

    // create empty array for module ids
    $module_ids = array();
                 
    // loop through modules and add them to array
    foreach( $modules as $module ) :
        $module_ids[] = $module->ID;
    endforeach;
                 
    // get the current index
    $current_index = array_search( $current_post_id, $module_ids );
                 
    // find the prev/next items
    $prev_module = $current_index - 1;
    $next_module = $current_index + 1;
                 
    // find first and last modules
    $first_module = $module_ids[0];
    $last_module = end($module_ids);
  • For those that come searching for answers facing a similar issue as me. It ended up being as simple as adding the below line before getting the current index.

    
     $current_post_id = get_the_ID();
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

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.