Home Forums General Issues Relationship Field and Prev/Next Posts


Relationship Field and Prev/Next Posts

  • Despite what I’ve read and found here and elsewhere, I’m still having trouble figuring out how to get prev/next posts to work with relationship fields on a site and I was hoping for some guidance.

    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 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. However, the way I have it set now, it obviously just navigates the loop based on the taxonomy and post date as opposed to the order set by the relationship field on the artist page.

    <div class="previous-post-link">
      <?php previous_post_link('%link', '<div class="prev-link"><i class="fas fa-caret-left"></i></div>', $in_same_term = true, $excluded_terms = '', $taxonomy = 'assignedartist'); ?>
    <div class="next-post-link">
      <?php next_post_link('%link', '<div class="next-link"><i class="fas fa-caret-right"></i></div>', $in_same_term = true, $excluded_terms = '', $taxonomy = 'assignedartist'); ?>    

    For some reason, I can’t wrap my head around how to query from the single-artist.php (“artist” post type) to the single-artwork.php (“artwork” post type — relationship field, “artworks” ) page and relationship field so I can create Previous and Next links to navigate between artworks. I can manage to pull the page info like title (artist name) but not the relationship field or its order.

    I reviewed this post, Next post in relationship field and pages like, ACF Relationship Field Prev/Next Buttons, but their setups are just different enough that I get lost. The latter seems to be limited to a single page, but I have several artist pages. The artwork needs to navigate only the artworks featured on whichever artist page it appears. Any help or direction would be greatly appreciated.

  • I also feel like ACF Relationship Field Prev/Next Buttons would work but I get stuck on the first part in how to pull from all artist pages or the single-artist.php template as opposed to just the page ID in…

    // This should be the ID of the page the relationship field is set up on,
    // NOT this page’s ID
    $page_id = 5;
    // Get the projects from the relationship field
    $project_listing = get_field('project_listing', $page_id);
  • You cannot use the built in previous and next post links because the post being shown is not related to other posts in the DB, it is related to other posts in a relationship field in ACF.

    The major issue here is “what relationship field”. You could have several relationship fields. How do you know that someone clicked on a related post link in another post to get to the current post? What relationship field did was used to show that link?

    So you need 2 pieces of information that are not readily available when a post is shown.

    1. The originating post where the related posts were originally shown
    2. the relationship fields used to show those related posts

    Given this information you would get the relationship field from the original post and loop though the posts in that relationship field to determine the post before and after the current post in that field.

  • Thank you for the response, John. For those that are looking for more details and how I eventually resolved it. I added it to a previous post regarding this issue.

    Next post in relationship field

    There are two posts online that will offer solutions. One in mentioned in that post and the one I eventually used with some minor changes. The second option can be found HERE, as linked above.

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

You must be logged in to reply to this topic.