waffl
waffl

Reputation: 5511

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();

Upvotes: 4

Views: 11810

Answers (3)

BenJonroc
BenJonroc

Reputation: 123

While waffl's solution works, I prefer to use "tax_query" Taxonomy Parameters because tax_query is specifically designed to query by taxonomy.

$term = get_queried_object();
$termID = $term->term_taxonomy_id;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = array(
    'tax_query' => array( 
            array(
                'taxonomy' => 'category', //you change this to a custom taxonomy if needed
                'field' => 'term_id',
                'terms' => $termID
            )
        ),

    'paged'=> $paged,
    'posts_per_page' => '15'
);
$pageposts = new WP_Query($query);

Upvotes: 0

Mark
Mark

Reputation: 23

I realize this is old, but I had the same problem. I used a method similar to what you came up with for my category archive, but I needed to use WP Query for search.php as well, which led me to looking for a solution. The wordpress codex has a way to preserve the original query for search, and it seems to work for a category archive as well:

<?php
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);
?>

http://codex.wordpress.org/Creating_a_Search_Page#Preserving_Search_Page_Results_and_Pagination

Should be able to just add the arguments you need and be good to go.

Upvotes: 1

waffl
waffl

Reputation: 5511

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);

Upvotes: 7

Related Questions