0

So I made a custom taxonomy like this and named it blog-category

function homekong_blog_categories() {
    $labels = array(
        'name'              => _x( 'Blog Categories', 'taxonomy general name', 'homekong' ),
        'singular_name'     => _x( 'Blog Category', 'taxonomy singular name', 'homekong' ),
        'search_items'      => __( 'Search Blog Categories', 'homekong' ),
        'all_items'         => __( 'All Blogs Categories', 'homekong' ),
        'parent_item'       => __( 'Parent Blog Category', 'homekong' ),
        'parent_item_colon' => __( 'Parent Blog Category:', 'homekong' ),
        'edit_item'         => __( 'Edit Blog Category' , 'homekong'),
        'update_item'       => __( 'Update Blog Category', 'homekong' ),
        'add_new_item'      => __( 'Add New Blog Category' , 'homekong'),
        'new_item_name'     => __( 'New Blog Category' , 'homekong'),
        'menu_name'         => __( 'Blog Categories' , 'homekong'),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'blog-category' ),
    );

    //register taxonomy
    register_taxonomy( 'blog-category', 'blogs', $args );
}
add_action( 'init', 'homekong_blog_categories', 0 );

Then I wrote a wp_query on my archive-blogs page (Here blogs is a custom post type)

So it displayed all my post but when I try to visit category wise it just displays post from Everything page where it shows all posts

This is my query to display the posts

<?php
// args
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'order_by' => 'publish_date',
'order' => 'desc',
'post_type' => 'blogs',
'paged' => $paged,
'taxonomy' => 'blog-category',
'operator' => 'EXISTS',
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>

<?php $counter = 0;?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-lg-6">
    <div class="blog-item">
        <div class="img-thumb">
            <?php the_post_thumbnail('full');?>
        </div>
        <div class="blog-text">
            <div class="blog-cat"><label for="">
                <?php echo blog_categories_terms($post->ID, 'blog-category');?>
            </label></div>
            <a href="<?php the_permalink();?>">
                <h3 class="blog-title">
                <?php the_title();?>
                </h3>
            </a>
            <div class="blog-date"><?php echo get_the_date();?></div>
        </div>
    </div>
</div>

<?php endwhile; ?>

<?php endif; ?>
<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

And finally this is how i display the Category/terms to click onto

<?php
$terms = get_terms('blog-category');
if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo "<li>" . '<a href="' . get_category_link($term) . '" class="active">' . $term->name . '</a>' . "</li>";
    }
}
?>

So when I am clicking those links it adds the url to the top but the page is not filtering category wise

  • 2
    Your archive templates should not have new WP_Query on them. WordPress has already queried the correct posts and you loop through them with have_posts() :the_post();. – Jacob Peattie Sep 16 at 6:02

Your Answer

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

Browse other questions tagged or ask your own question.