BB Design
BB Design

Reputation: 705

Limit main WordPress loop by category

In the archive.php file of my custom theme, I want to only show posts of a certain category. It was suggested elsewhere that I add this before if( have_posts() ):

$posts = get_posts('category=1');

However, that seems to disregard all the other filtering that may be in place (such as post date, post author, etc.).

I don't want to remove any other filtering that is already in place. All I want to do is say: "AND ONLY SHOW POSTS WITH CATEGORY=1". Doable?

Upvotes: 1

Views: 213

Answers (1)

Mark Leiber
Mark Leiber

Reputation: 3138

You could try merging your new filters into the existing filters:

global $wp_query;
$posts = get_posts(
  array_merge(
    array('category' => '1'),
    $wp_query->query
  )
);

Upvotes: 1

Related Questions