Knowledgebase

Get Archives Page Template to Show Specific Category

Intro

  To get the archives.php to display only specific categories, you need to change the query on line 5: $fetch_archive = $wpdb->get_results(“SELECT * FROM ” . $wpdb->posts . ” WHERE post_status=’publish’ AND post_type = ‘post’ GROUP BY $wpdb->posts.ID ORDER BY post_date DESC”); Replace the above with this query. I have reformatted it so you can […]

Solution

 

To get the archives.php to display only specific categories, you need to change the query on line 5:

$fetch_archive = $wpdb->get_results("SELECT * FROM " . $wpdb->posts . " WHERE post_status='publish' AND post_type = 'post' GROUP BY $wpdb->posts.ID ORDER BY post_date DESC");

Replace the above with this query. I have reformatted it so you can see what each line is doing:

$fetch_archive = $wpdb->get_results("
SELECT *
FROM " . $wpdb->posts . "
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->term_taxonomy.term_id IN(1,2,3)
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND post_status='publish'
AND post_type = 'post'
GROUP BY $wpdb->posts.ID
ORDER BY post_date DESC");

On the WHERE line, replace the red category IDs with the ids corresponding to the categories you want to appear. To get the ID, you can either install Admin Managment Xtended or hover over the category link under Posts > Categories to see the “Tag_ID in the status bar of your browser.

 

Get 15% off!

Signup and get 15% off any of our Ghost or Framer themes immediately