I’m building a WordPress website, with a video custom post type. All videos are displayed on a paginated archive page, and open in a modal when clicked.
Because the site search returns single video posts in search results, I’ve added a function to redirect to the archive page when the single is accessed. I also append the post ID to the URL, and in the archive page I have code that checks for a ‘show’ parameter to trigger the modal to open:
add_action( 'template_redirect', 'custom_post_single_redirect_management' );
function custom_post_single_redirect_management() {
global $post;
if(is_singular( 'video' )) {
wp_redirect( get_post_type_archive_link('video').'?show='.$post->ID, 301 );
exit;
}
}
So far so good, and all is working fine.
The problem I now have, and I’m hoping you can help with, is when the video being accessed isn’t on page 1, I can’t open the modal to show the video.
Does anyone have a suggestion for handling this? Is it possible from within my function above, to check which page the video will be on within the archive page, and then add a new parameter on the redirect? i.e. '?show='.$post->ID.'&page=2'
Thanks for your help!
I’m not sure if I’m committing a faux pas by answering my own question, but perhaps it will help others who stumble upon this question.
I could not find an easy way to find the pagination number. Instead I used pre_get_post() in my functions.php to check for the ‘show’ parameter, and if it was being passed to the page, I used stick_post() to make the post ID passed in the ‘show’ parameter sticky.
In order to get my custom post type archive to show the sticky, I used this snippet. Lastly I used unstick_post() in my template to remove the ‘stickiness’.
Altogether this brought the post in question to page 1 in my archive, which then opened in the modal. A bit hacky, but it works.
If there’s a better way to achieve this please post it and I’ll accept your answer as best.