A way to detect which page a post is on

I’m wondering if there is a good way to tell which archive page a post came from. I essentially just need the post’s position in the total order, then divide it by the ‘posts_per_page’ option. The hangup I’m having is getting that position or offset of where the post sits.

EDIT: All while being on the SINGLE POST template.

There, no matter what, the usual $wp_query global and ‘page”https://wordpress.stackexchange.com/”paged’ query vars are always 0 – so those won’t get me anywhere.

5 Answers
5

Alright, nevermind. I got this solved by doing this at the top of the single post template:

$position_query = array( 'post_type' => 'portfolio', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => -1 );
$position_posts = get_posts($position_query); $count = 0;
foreach ($position_posts as $position_post) { $count++;
    if ($position_post->ID == $current_id) { $position = $count; break; }
}
$posts_per_page = get_option('posts_per_page');
$result = $position/$posts_per_page;
$current_page = ceil($result);

Leave a Comment