Default WP Gallery – show only galleries of a certain category

What I suspect is that those Gallery Categories are not the category taxonomy. They are probably a registered taxonomy, registered by the same script which added the gallery post type.

To find out, click on the Gallery Categories ( in the provided image ) and you will be redirected to a URL such as this:

http://example.com/wp-admin/edit-tags.php?taxonomy=my-taxonomy&post_type=gallery

Check the taxonomy argument. If it is anything other than category, then you are on a custom taxonomy, and you need to add a tax_query to your WP_Query. Here’s how to do it:

Your args would simply change from this:

$port=array('post_type' => 'gallery', 'category_name' => 'beach', 'showposts' => -1 );

To this:

$port = array(
     'post_type' => 'gallery', 
     'showposts' => -1,
     'tax_query' => array (
          array(
              'taxonomy' => 'my-taxonomy',
              'field'    => 'slug',
              'terms'    => 'beach',
          )
     )
);

Side Note:

Make sure you use wp_reset_postdata(); just before the return $html;. This will reset the query and makes sure your shortcode won’t affect any other queries in the page.

All done.

Leave a Comment

tech