0

I'm trying to "mount" a loop for category.php in which it is based on the current category and meta key.

But the loop insists on showing posts from another category

global $taxonomy,
        $term;

$cats       = get_the_category();
$newQuery   = new WP_Query( array( 
    'cat'           => $cats[0]->term_id,
    'meta_key'      => 'premium_model',
    'meta_value'    => true,
    'posts_per_page'=> 24, 
    'orderby'       => 'rand'                             
) );  

if( $newQuery->have_posts() ) {

    while( $newQuery->have_posts() ) { 

        $newQuery->the_post(); 
        // Post Here
    }
}

Can you tell me where I'm wrong?

0

get_the_category() will retrieve the category of current loop item. In your case, it's probably the last post of your main archive query.

To get the category of currently displayed category in the archive page, you can use get_queried_object_id():

// Check if this is a category archive
if ( is_category() ) {

    // Get the current category ID
    $category_id = get_queried_object_id();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.