Custom Taxonomy Query
written by: Jeff McNearCustom Taxonomy Query
Sorted by radio button selection and then date…
Here is the query:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'meta_key' => 'landing_page_priority_grade',
'orderby' => array( 'meta_value' => 'ASC', 'date' => 'DESC' ),
'tax_query' => array(
array(
'taxonomy' => 'focus-group', // the custom vocabulary
'field' => 'slug',
'terms' => get_field('focus_taxonomy_slug'), // provide the term slugs
),
),
'post_status' => array( 'publish' ),
'facetwp' => false,
'paged' => $paged
);
?>
First of all I want this query to have pagination as described here : https://wpza.net/how-to-paginate-a-custom-post-type-in-wordpress/ which requires I begin with this line:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
then we start the arguments for the query
$args = array(
we are looking for published posts, and want to show 10 per page
'post_type' => 'post',
'post_status' => array( 'publish' ),
'posts_per_page' => 10,
we want to specify the slug of a custom taxonomy via an ACF field named “focus_taxonomy_slug” that the editor populates in the editor
'tax_query' => array(
array(
'taxonomy' => 'focus-group', // the custom vocabulary
'field' => 'slug',
'terms' => get_field('focus_taxonomy_slug'), // provide the term slugs
),
),
This particular project is also using FacetWP to filter search results and sometimes this can make queries behave in peculiar ways if facets isn’t explicitly nullified … so we will do so
'facetwp' => false,
there is an ACF radio button field which has numeric values attached to it which allows us to divide content into prioritized sections – we look for this meta information
'meta_key' => 'landing_page_priority_grade',
and then sort first by that value and then by the published date
'orderby' => array( 'meta_value' => 'ASC', 'date' => 'DESC' ),
now we are in the position to start the loop
<?php
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
call up some info
<h2><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_field('alternate_title_over_ride'); ?></a></h2>
<?php the_excerpt(); ?>
close the loop
<?php endwhile; ?>
and drop in the pagination
<nav class="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'prev_text' => '« Previous',
'next_text' => 'Next »'
) );
?>
</nav>