• Hi,
    I am looking for a way to only show certain categories in the archive widget. The reason I want to do this is because I have a lot of photographs on my webpage and also some blog posts and because each photograph is a post, it is very hard to find the blog posts in the archive. I am using the organic photographer theme. You can have a look at my webpage here: https://www.lichtgezaubert.de/. The page is German.The photographs have a separate menu (called Fotos) and there is also a menu entry for the blog posts called Blog.

    This blog post describes how to add custom functions to the themes functions.php to only show certain categories in the archive widget: https://illuminatikarate.com/blog/wordpress-how-to-exclude-categories-from-the-archive-widget/

    I added the following code to functions.php:

    //add the ik archive filters
    add_filter('getarchives_where','ik_custom_archives_where',10,2);
    add_filter('getarchives_join','ik_custom_archives_join',10,2);

    //add custom SQL to the archives widget JOIN clause
    function ik_custom_archives_join($sql){
    global $wpdb;
    $sql = $sql . "LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) ";
    $sql = $sql . "LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
    return $sql;
    echo $sql;
    return $sql;
    }

    //add custom SQL to the archives widget WHERE clause
    function ik_custom_archives_where($sql){
    global $wpdb;
    //get categories IDs from slugs
    $include_cat_1 = get_category_by_slug("blog");
    echo ''; var_dump( $include_cat_1 ); echo '';
    //list of cats to exclude
    //add more ID's to this comma-separate list to include more categories
    $include_list = $include_cat_1->cat_ID;
    $sql = "WHERE post_type = 'post' AND post_status = 'publish' ";
    $sql = $sql . "AND $wpdb->term_taxonomy.term_id IN ($include_list)";
    echo $sql;
    return $sql;
    }

    However, when I add this code to functions.php, the archive widget does not show anything.

    I added some debug statements. I get the follwing output:

    object(stdClass)#4295 (15) {
    [“term_id”]=>
    &string(2) “10″
    [“name”]=>
    &string(4) “Blog”
    [“slug”]=>
    &string(4) “blog”
    [“term_group”]=>
    string(1) “0″
    [“term_taxonomy_id”]=>
    string(2) “10″
    [“taxonomy”]=>
    string(8) “category”
    [“description”]=>
    &string(0) “”
    [“parent”]=>
    &string(1) “0″
    [“count”]=>
    &string(1) “0″
    [“cat_ID”]=>
    &string(2) “10″
    [“category_count”]=>
    &string(1) “0″
    [“category_description”]=>
    &string(0) “”
    [“cat_name”]=>
    &string(4) “Blog”
    [“category_nicename”]=>
    &string(4) “blog”
    [“category_parent”]=>
    &string(1) “0″
    }

    WHERE post_type = ‘post’ AND post_status = ‘publish’ AND wp_term_taxonomy.term_id IN (10)

    LEFT JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id) LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)

    I am new to WordPress. Can anyone give me some hints what’s going wrong here or how to debug the problem?
    Best regards,
    Michael

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Showing only posts from certain categories in archive widget’ is closed to new replies.