Showing only posts from certain categories in archive widget
-
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
- The topic ‘Showing only posts from certain categories in archive widget’ is closed to new replies.