Skip to content

Instantly share code, notes, and snippets.

@crispyabsurdist
Created February 13, 2023 07:16
Show Gist options
  • Save crispyabsurdist/ce824e7915658bd5c70ccbc7c30daff1 to your computer and use it in GitHub Desktop.
Save crispyabsurdist/ce824e7915658bd5c70ccbc7c30daff1 to your computer and use it in GitHub Desktop.
<?php
public function postFilter()
{
// Define custom post type and taxonomies
$post_type = 'product';
$taxonomies = array('brand', 'category');
// Get the current page number
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Define the query arguments
$args = array(
'post_type' => $post_type,
'posts_per_page' => 10,
'paged' => $paged,
'tax_query' => array()
);
// Get the taxonomy terms from the URL
foreach ($taxonomies as $taxonomy) {
if (isset($_GET[$taxonomy]) && !empty($_GET[$taxonomy])) {
$args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $_GET[$taxonomy]
);
}
}
// Run the query
$query = new WP_Query($args);
// Check if there are any posts
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display the post
}
// Pagenation
$big = 999999999; // need an unlikely integer
} else {
// No Posts Found
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment