Opened 14 years ago
Closed 14 years ago
#14846 closed defect (bug) (invalid)
in_category returning true when last post is from specified category
Reported by: | enrique-ramirez | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | major | Version: | 3.0.1 |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
I'll try to be as clear as possible with this. I've added a single line to my index.php file for testing purposes, right before I start my loop:
<?php if ( in_category('8') ) { ?>Category 8<?php } else { ?>Blog<?php } ?> <?php query_posts('&cat=-8'); ?> ... loop here...
Please note I do NOT display posts in category 8 in my index.php file. Posts with category 8 are displayed in another page, hidden from the main index.
Everything works as expected. When I go to index.php I can see my blog index and the word "Blog" being printed out, and posts from category 8 are not displayed.
Problem is when, on my WP Admin panel, the LATEST post is from category 8. Even when it does not appear on the index page, the word "Category 8" is printed instead of blog.
Once again, this is the post index page. I'm NOT in the single.php template, nor in a category specific page (so is_category() is out of the question).
So, in resume: If the latest post added is from category 8, index.php assumes it is IN category 8, even though it's the post index (not a Category specific page, nor a single post page) AND despite the post NOT even being displayed; the in_category condition is not even inside the loop.
Quite an issue since I'm using in_category for different sidebars, which are being completely ruined by this one issue.
Note:
Issue is fixed if the LATEST post is viewable by the user. For example, if I add a private post after the latest "Category 8" post and am logged in, in_category returns false. If, on the contrary, I check index.php logged out, in_category returns false (even though the latest post is outside category 8, I don't have permission to view it and in_category returns false).
Another note: it doesn't matter if the post index displays the posts or not. If I have permission to access the latest post, in_category will return true if the latest one is from category 8 and false if it's somewhere else.
Change History (5)
#2
@
14 years ago
Hi nacin,
No. Even when inside the loop, the results are the same. Tried it before but forgot to mention it, sorry.
#3
@
14 years ago
If you call query_posts() like that and your loop excludes category 8, then in_category(8) will never be true. Are you saying that's exactly what's happening? This should produce a string of bool(false)'s.
query_posts('cat=-8'); if ( have_posts() ) while( have_posts() ) : the_post(); var_dump( in_category(8) ); endwhile; endif;
#4
@
14 years ago
Ok, not quite.
It IS true the above string returns a string of bool(false)'s, but I need this outside each post, not once per post.
Say, for example, I have this code:
<h1><?php if ( in_category('8') ) { ?>Category 8<?php } else { ?>Blog<?php } ?></h1> <?php query_posts('&cat=-3,-8'); ?> <?php if (have_posts()) : ?> <ol> <?php while (have_posts()) : the_post(); ?> <li>... </li> <?php endwhile; ?> </ol> <?php endif; wp_reset_query(); ?>
I don't need an <h1> per post, since this is the "section" title.
Once again, this is just an example. I'm actually using this to get different sidebars per section in my sidebar.php file, but it gives you the idea I need the true or false value outside the actual post.
#5
@
14 years ago
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
I'm confused what you're trying to do. in_category() is designed to be used within the loop, to ascertain whether said post is in the category specified. If it is used outside the loop, it will inevitably check whichever the first post is. Since you're calling it before query_posts(), then that would be the first post of the original loop.
Are you thinking of is_category() -- whether you're on a category archive page? You may have better luck in the support forums: http://wordpress.org/support/.
I believe in_category() needs to be used in the loop. Otherwise WP has no idea what post you want to refer to, thus it pulls the first one out of $posts.
If you call query_posts() before the check, it'll work, because the $posts array will be set.