Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter mthaler

    (@mthaler)

    I tried the following function to include only posts from the “blog” category and all subcategories of the “blog” category:

    //add custom SQL to the archives widget WHERE clause
    function ik_custom_archives_where($sql){
    global $wpdb;
    //get categories IDs from slugs
    $include_cat = get_category_by_slug(“blog”);
    $include_cat_children = get_categories( array( ‘child_of’ => $include_cat ) );
    //echo ”; var_dump( $include_cat_children ); echo ”;
    //list of cats to include
    //add more ID’s to this comma-separate list to include more categories
    $include_list = $include_cat->cat_ID;
    foreach ($include_cat_children as $category) {
    $include_list = $include_list . ‘, ‘ . $category->cat_ID;
    }
    echo $include_list;
    $sql = “WHERE post_type = ‘post’ AND post_status = ‘publish’ “;
    $sql = $sql . “AND $wpdb->term_taxonomy.term_id IN ($include_list)”;
    //echo $sql;
    return $sql;
    }

    But now I have all posts in the archive widget again, not only the ones from the “blog” category and the “blog” subcategories. Interestingly, if I just hardcode one of the subcategory ids in the query, e.g. $sql = $sql . “AND $wpdb->term_taxonomy.term_id IN (16)”; I get all posts in the archive widget, not only the ones belonging to the subcategory with id 16. Does anybody have any idea what’s going on here?
    Best regards,
    Michael

    Thread Starter mthaler

    (@mthaler)

    o.k., I found the problem: there are simply no posts in the blog category, there are all in subcategories. Now I just have to find out how to get a list of subcategories, because I don’t want to update the functions.php file everytime I add a subcategory.
    Best regards,
    Michael

Viewing 2 replies - 1 through 2 (of 2 total)