Sometime we need to exclude specic posts and pages from wordpress archive pages. If there are some internal pages behind auth, you may want to exclude these from archive pages so that these don’t get indexed in search engines like Google. Here is wordpress code you can use for this:
function pre_get_posts_date_archive($query) { // List of posts/pages to be excluded. $exclude_array = array(1000, 1001); // For archives, year and monthnum should be present in query object if ($query->get('year') && $query->get('monthnum')) { $query->set( 'post__not_in', $exclude_array ); } } add_action('pre_get_posts', 'pre_get_posts_date_archive');
Here we piggy back on pre_get_posts hook and alter the query in case it is for a year-month archive page.