skip to Main Content

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. I also feel like the latter 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);

Any help or direction would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I've tried many solutions but none have worked yet. The only way I even get a hint of success is by adding a ACF field to the artworks post type that selects the artist post type via a post object field. As @chris-haas suggested, that hint was found via This Link

    There seems to be an issue with the following code. I'm note sure how $current_post_id applies to the array_search. 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. $first_module and $last_module show the correct IDs too. I just can't seem to get the current post ID to then detemine 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);
    

    I'm a bit at a loss.


  2. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search