filters – ACF Relationship Previous Next links of CPT
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.
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. I was using THIS PAGE as my guide.
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 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; $last_module = end($module_ids);