Quantcast
Channel: WordPress.org Forums » All Posts
Viewing all articles
Browse latest Browse all 112476

jhoffm34 on "[Loop] Multiple loops on a category template"

$
0
0

The best way to do this is through pre_get_posts. Altering the main query through query_posts can get messy quick. What pre_get_posts allows you to do is alter the main query before it is even run, so you can exclude those category posts no matter where the main query is called.

The is_main_query function is used to hook into just the primary query when you use pre_get_posts, so that you will not affect any other queries that you have set up.

In your functions.php (or even better, maybe through a plugin?) you will run something like this. Just change the category number to your category ID, and leave the negative sign in there to signal that you would like to exclude this category.

add_action( 'pre_get_posts', 'exclude_popular_posts );
function exclude_popular_posts( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
        $query->set( 'cat', '-5' );
}

Viewing all articles
Browse latest Browse all 112476

Trending Articles