I’ve got post type called “Portfolio” and single-portfolio.php file to handle it (it’s WordPress). When I use there something like that it works like expected:
$post_id = $post->ID; //returns ID of current portfolio post. Good!
BUT when I post short query like this in the middle:
$post_id = $post->ID; //returns ID of current portfolio post. Good!
wp_reset_query();
query_posts('posts_per_page=4');
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_id(); //returns ID of standard blog post
endwhile;
endif;
wp_reset_query();
$post_id = $post->ID; //returns ID of last BLOG post. Wrong!
I’m only concerned about $post_id variable in above example. I want it to always return correct ID of current PORTFOLIO post and not be dependent on other queries. How do I achieve that?
The
wp_reset_queryfunction does reset the global$postvariable as well, but only based on the global$wp_queryvariable. That still is modified, probably due to one of the little flaws in WordPress. In your case I’d say a simpleWP_Query::rewind_posts()should do it:Also you should consider to create a second loop, not overwrite the first one.
See as well: