Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created June 26, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/592411250688ec76730bdb0142812836 to your computer and use it in GitHub Desktop.
Save aaronsummers/592411250688ec76730bdb0142812836 to your computer and use it in GitHub Desktop.
Dynamic post / custom post type filter using taxonomies, categories and tags.
<?php
$post_type = 'posts';
$post_count = 6;
/**
* @var [$current_post_type_taxonomies] Get all the taxonomies registered in wordpress
* @var [$actual_taxonomies] Remove post_format from the taxonomies
* @var [$tax_terms_array] Create an empty array for the taxonomy terms, this later is used to populate the tax_query in the loop
*/
$current_post_type_taxonomies = get_object_taxonomies( $post_type, 'names' );
$actual_taxonomies = ( in_array('post_format', $current_post_type_taxonomies) ) ? array_diff( $current_post_type_taxonomies, array('post_format') ) : $current_post_type_taxonomies;
$tax_terms_array = array(); // taxonomy arguments are set within the loop
if ( !empty($actual_taxonomies) ) :
$filters_wrapper = array('<div class="filters"><br id="filters"><form action="#filters" method="GET"><h3 class="button arrow purple200">You may want to use these filters</h3>', '</form></div>');
echo $filters_wrapper[0];
/**
* Automatgically create the filters
*/
foreach ( $actual_taxonomies as $atax ) :
// Create the Placeholder text for chosen dropdown menus.
$placeholder = str_replace('_', ' ', $atax);
// Simple way to make a string plural
$last_letter = substr($atax, -1);
if ( $last_letter != 's' ) :
// If it ends in a "Y" replace that with "IES" other wise add an "S"
$placeholder = ( $last_letter == 'y' ) ? substr($placeholder, 0, -1) . 'ies' : $placeholder . 's';
endif;
// Select menu wrapper
// Using jQuery chosen plugin, so requires a data-attr and empty option to display the placeholder.
$select_tag = array('<div class="filter"><select name="' . $atax . '" id="' . $atax . '" data-placeholder="All ' . $placeholder . '">', '</select></div>');
/**
* Use get_terms to grab all the taxonomies including the default tag and category.
* These then create the options for the dropdown menu
*/
$taxonomies = get_terms( array(
'taxonomy' => $atax,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
) );
echo $select_tag[0];
echo '<option value=""></option>'; // Empty option for placeholder
foreach ( $taxonomies as $tax ) :
$taxonomy_slug = $tax->slug;
$taxonomy_name = $tax->name;
$selected = ( isset($_GET[$atax]) && !empty( $_GET[$atax] ) && $_GET[$atax] == $taxonomy_slug) ? 'selected' : '';
echo '<option value="' . $taxonomy_slug . '" ' . $selected . '>' . $taxonomy_name . '</option>';
endforeach; // foreach ( as ) :
echo $select_tag[1];
/**
* Check the query string for our custom taxonomies.
* Skip the default category and post_tags...
* If the taxonomy has a term in the query string create the terms array
*/
if ($atax == 'category' || $atax == 'post_tag') continue;
if ( isset($_GET[$atax]) && !empty( $_GET[$atax] ) ) :
$tax_terms_array[] = array(
'taxonomy' => $atax,
'field' => 'slug',
'terms' => array( $_GET[$atax] ),
);
endif;
endforeach; // foreach ( $actual_taxonomies as $atax ) :
echo '<button type="submit" class="submit button"><img src="' . get_template_directory_uri() . '/assets/img/arrow/right.svg"></button>';
echo $filters_wrapper[1];
endif; // if ( !empty($remove_post_format) ) :
/**
* Start the post loop
*
* @return [type] [return description]
*/
// Set the args for the post loop and begin the query.
$paged_key = ( is_front_page() ) ? 'page' : 'paged';// Front page uses "page" not "paged"
$paged = ( get_query_var( $paged_key ) ) ? get_query_var( $paged_key ) : 1;
$default_args = array(
'post_type' => $post_type,
'posts_per_page' => $post_count,
'paged' => $paged
);
/**
* Build the arguments for the post loop
*
* @var [$tax_relation] merge the relationship into the taxonomy array
* @var [$tax_args, $cat_args, $tag_args, $args] create all the arguments for each of the taxonomies.
*/
$tax_relation = array_merge($tax_terms_array, array('relation' => 'AND'));
$tax_args = ( !empty( $tax_terms_array ) ) ? array( 'tax_query' => $tax_relation ) : array();
$cat_args = ( isset($_GET['category']) && !empty( $_GET['category'] ) ) ? array('category_name' => $_GET['category'] ) : array('');
$tag_args = ( isset($_GET['post_tag']) && !empty( $_GET['post_tag'] ) ) ? array('tag' => $_GET['post_tag'] ) : array('');
$args = array_merge($default_args, $cat_args, $tag_args, $tax_args);
// get the posts and loop the shit out of it.
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array_filter($args) ); # array_filter out the empty arrays if they exist.
// Start the loop
if ( $wp_query->have_posts() ) :
echo '<div class="post-wrapper">';
// Time for the Post loop
while ($wp_query->have_posts()) : $wp_query->the_post();
echo '<div class="post clearfix">'
. get_the_post_thumbnail( $post->ID, 'column_image_full', array( 'class' => 'alignleft' ) )
. '<article>'
. '<h3>' . get_the_title() . '</h3>'
. '<div class="all-terms">' . wpdocs_custom_taxonomies_terms_links() . '</div>'
. wpautop(excerpt(25))
. '<a href="' . get_the_permalink() . '" class="post-link"><span class="logo-button">More details</span></a>'
. '</article>'
. '</div>';
endwhile; // foreach ($post_loop as $pl ) :
// Using https://wordpress.org/plugins/wp-paginate/
// But could be standard pagination here
if(function_exists('wp_paginate')):
wp_paginate();
endif;
$wp_query = null;
$wp_query = $temp; // Reset
echo '</div>';
else :
echo '<h2>Sorry no posts could be found, please try a different search!</h2>';
endif; // if ( $wp_query->have_posts() ) :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment