Search code examples
wordpressloopsarchivecategories

Wordpress - Use wp_query in category archive - how to display the appropriate category?


I am using wp_query in a category archive so that I can use the meta_query to ignore posts with certain meta values.

The problem is that since I am using wp_query, it seems to ignore the category that is currently being viewed and displays all the categories.

Is there a way to retrieve the category (perhaps as defined by the url) that the user is viewing and insert it into the wp_query argument array?

I've seen this suggested solution on stack overflow, but there must be a simpler way to do it since when no the default loop is used, wordpress automatically displays the correct category.

The code currently:

$query = array(
'meta_query' => array(
        array(
            'key' => 'Display',
            'value' => 'Yes',
        )
    ),
    'paged'=> $paged
);

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$pageposts = new WP_Query($query);

if ($pageposts):
while ( $pageposts->have_posts() ) : $pageposts->the_post();

Solution

  • Well, this is the best solution I could come up with on my own (using single_cat_title to set the variable):

    $currentCategory = single_cat_title("", false);
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = array(
        'category_name' => $currentCategory,
    
        'paged'=> $paged,
        'posts_per_page' => '15'
    );
    $pageposts = new WP_Query($query);