Filter custom post query by tag in page template

I’m working on a little site with a directory of baby names. I have two pages to show boys names or girls names, and they each have their own separate page template.

The templates include a post query which gets the posts from the ‘baby-names’ custom post type under the appropriate category (boys-names/girls-names) and paginates them, but I’m looking for a way to filter them by letter (using the post tags).

Here’s the code for the boys names page template:

<div id=”primary” class=”content-area”>
<main id=”main” class=”site-main”>

<div class=”a-z-wrapper”>
<span class=”a-z-all”>All</span>
<span class=”a-z-a”>A</span>
<span class=”a-z-b”>B</span>
<span class=”a-z-c”>C</span>
<span class=”a-z-d”>D</span>
<span class=”a-z-e”>E</span>
<span class=”a-z-f”>F</span>
<span class=”a-z-g”>G</span>
<span class=”a-z-h”>H</span>
<span class=”a-z-i”>I</span>
<span class=”a-z-j”>J</span>
<span class=”a-z-k”>K</span>
<span class=”a-z-l”>L</span>
<span class=”a-z-m”>M</span>
<span class=”a-z-n”>N</span>
<span class=”a-z-o”>O</span>
<span class=”a-z-p”>P</span>
<span class=”a-z-q”>Q</span>
<span class=”a-z-r”>R</span>
<span class=”a-z-s”>S</span>
<span class=”a-z-t”>T</span>
<span class=”a-z-u”>U</span>
<span class=”a-z-v”>V</span>
<span class=”a-z-w”>W</span>
<span class=”a-z-x”>X</span>
<span class=”a-z-y”>Y</span>
<span class=”a-z-z”>Z</span>


$temp = $wp_query;
$wp_query = null;
$args = array(
‘post_type’ => ‘baby-names’,
‘post_status’ => ‘publish’,
‘category_name’ => ‘boys-names’,
‘posts_per_page’ => 25,
‘orderby’ => ‘title’,
‘order’ => ‘ASC’,
‘paged’ => $paged,
$wp_query = new WP_Query($args);
?> <ul id=”name-list”> <?php
while ($wp_query->have_posts()) : $wp_query->the_post();
<li id=”name-<?php the_ID(); ?>” <?php post_class(); ?>>
<?php the_title(); ?>

<div class=”name-navigation”>
<div class=”alignleft”><?php previous_posts_link(‘&laquo; Previous’) ?>
<div class=”alignright”><?php next_posts_link(‘Next &raquo;’) ?></div>

$wp_query = null;
$wp_query = $temp; // Reset


</main><!– #main –>
</div><!– #primary –>


Adding ‘tag’ => ‘x’ to the query args works, maybe there’s a way to get this from a url query using the html a-z elements?

Read more here:: Filter custom post query by tag in page template

Leave a Reply

Your email address will not be published. Required fields are marked *