The MU forums have moved to

Need help creating custom loop (4 posts)

  1. anointed
    Posted 12 years ago #

    I've spent the day working in order to get category exclusion for the homepage loop working.

    I have managed to get the backend UI working, so that a client can choose which categories to exclude in the theme admin panel.

    I checked the db options table and sure enough the data is being saved properly.

    example from the database sites options table -- all cats are either true or false like below

    zoo_cat_box_4 true
    --user checked box to exclude cat#4 in the admin

    zoo_cat_box_5 true
    --user checked box to exclude cat#5 in the admin

    zoo_cat_box_6 false
    --user did not check the box for cat#6 so it should show

    That means I have the db setup properly.
    (true next to cats that are excluded, false next to those not checked)

    ** 4,5,6 are the actual category id#'s, so that part is correct. I have an entry in the db for each category id#

    Where I am stuck is the function to display this on the homepage.

    Right now I have this function added to my themes functions file.

    // Custom Homepage loop
    function get_exclude_categories_loop() {
        $include = array();
        $counter = 0;
        $cats = get_categories('hide_empty=0');
        foreach ($cats as $cat) {
            if ( get_option( "zoo_cat_box_".$cat->cat_ID ) == 'true') {
                    $excludecats[] = $cat->cat_ID;
            $excludecats = implode(',',$excludecats);
        return $excludecats;

    On my homepage I am using this code to call the loop:

    query_posts(array('category__not_in' => array(get_exclude_categories_loop))); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();

    Now if I change the homepage loop code "get_exclude_categories_loop" and replace that with numbers like 4,5,6 then categories 4,5,6 do not show up.

    This means either my get_exclude_categories_loop function above is screwed up, or I'm not calling it correctly in the loop.

    Well I know I am REALLY close to getting this right, but 1/2 a day later I'm stuck...

    hoping a Ninja will recognize my mistake and let me know what I did wrong and why... (always trying to learn)


    How do I go about using var_dump on my get_exclude_categories_loop function to see what it's returning?

    I've tried everything I can think of to see what the function is spitting out, but can't get the hang of var_dump. Otherwise I would know if it's my function or how I am calling it in my home.php file...

  2. SteveAtty
    Posted 12 years ago #

    You function returns a array but then you do


    which means you've wrapped your array inside another.


    query_posts(array('category__not_in' => get_exclude_categories_loop() )); ?>

    To use var_dump you'd just put var_dump(get_exclude_categories_loop());

  3. cogmios
    Posted 12 years ago #

    or use a set

    $wp_query->set('cat', '-14,-1172,-867'); // replace 2nd parameter with array
    $wp_query->set('posts_per_page', 10);
    $posts = $wp_query->get_posts();
    include ($includedirectory . "loop.php");
  4. anointed
    Posted 12 years ago #

    Thank you guys, it's working now.

    Mostly it turns out that I was confused by the codex documentation when they gave the example:
    query_posts(array('category__not_in' => array(2,6)));

    That lead me to believe that I needed a comma separated list and not an array. With a few changes, all works perfectly now.

About this Topic

  • Started 12 years ago by anointed
  • Latest reply from anointed