I am trying to create a WP_Query on my category.php where it shows posts from a custom post type called "doctors" and it will query the posts based on the current slug in the url. So if it is at "website.com/categories/psychiatrists" it will show all of the custom post type "doctors" that have the category "psychiatrists"

I hope that makes sense. What I have right now however is a query that filters by the category title instead of the slug, which is causing a lot of issues if the title doesn't match the slug. When one of the SEO people go in and change the title of the category it breaks all of the links.

Any help would be super appreciated! I'm still a little new with this wordpress and php.

Here is the query I have right now:

            $args = array( 'post_type' => 'doctors',
            'category_name' => single_cat_title('',false),
            'posts_per_page' => -1 );

            $loop = new WP_Query( $args );                
              if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); 

                  get_template_part( 'content', 'providers' ); 

             endwhile; else : ?>
                <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
            <?php endif; ?>
  • Long time since this thread was open, but did you get this working and with pagination?
    – James B
    Apr 22, 2022 at 13:41

1 Answer 1


To get the category slug in a category archive page use:

$cat = get_queried_object();
$slug = $cat->slug;

get_queried_object fetches the currently queried object, e.g. the category object in a category page or the post object in a single post page.

  • Please mark this as the selected answer if it solved your problem by clicking the big tick mark on the left.
    – inarilo
    Aug 26, 2017 at 22:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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