Support » Fixing WordPress » in_category doesn’t work above the loop

  • Hey. It appears that after I asked about, and solved, a problem with in_category and these weird errors, I found out that in_category, no matter what, won’t work above the loop. Comes out with this error:
    Warning: Invalid argument supplied for foreach() in /home/.tahlula/plaincul/daysdifference.com/wp-includes/template-functions-category.php on line 438
    Warning: in_array(): Wrong datatype for second argument in /home/.tahlula/plaincul/daysdifference.com/wp-includes/template-functions-category.php on line 442
    It’s the same error I would get if there weren’t any posts in a category, or if there wasn’t a new post posted, or weird things like that. That happened either in the loop or under the loop, and those things were solvable. The difference now is that no matter what I’ve tried, in_category() won’t work above the loop.
    Why’s this? Is there a way I can solve this? Appreciate the help again, thanks. 🙂
    Update: submitted a bug report here, with WP 1.2 and 1.3 index.php files–sort of a before and after with in_category().

Viewing 4 replies - 1 through 4 (of 4 total)
  • I guess to make things new, putting the following code just below “require(‘./wp-blog-header.php’);” makes in_category() work:
    if ($posts) {
    foreach ($posts as $post) {
    $post_id_list[] = $post->ID;
    }
    $post_id_list = implode(‘,’, $post_id_list);
    $dogs = $wpdb->get_results(“SELECT DISTINCT
    ID, category_id, cat_name, category_nicename, category_description, category_parent
    FROM $tablecategories, $tablepost2cat, $tableposts
    WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)”);
    foreach ($dogs as $catt) {
    $category_cache[$catt->ID][] = $catt;
    }
    }
    …With one slight caveat: the category listing gets repeated. But at least that’s something. Could someone help me with the code to make it work like it should? You can check out the progress here: http://daysdifference.com. Thanks.

    in_category is an “in the loop” function.

    Well… as I found out last night, either in_category() is just for the loop or it’s deprecated in favor of is_category() and the whole other bunches of is_’s. But thanks for the reference.
    I guess we can consider this as resolved. Thanks for letting me know that, though.

    in_category() in loop
    is_category() is not in loop
    😉

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘in_category doesn’t work above the loop’ is closed to new replies.