Home › Forums › General Issues › Next post in relationship field › Reply To: Next post in relationship field
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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.