Support

Account

Home Forums General Issues Relationship Field and Prev/Next Posts

Solving

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>
    <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'); ?>    
    </div>

    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…

    <?php
    // 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.

Viewing 3 posts - 1 through 3 (of 3 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.