WP_Query with multiple filters ajax - php

Just started learning about filtering custom queries in Wordpress. I want to be able to filter through products (custom post types) by their taxonomies. I am able to get this working sort of. The problem is that when I apply more than one filter, only the last on works. I'm assuming the last one cancels the others out? How can I make all filters work simultaneously? Any help would be much appreciated!
sidebar.php:
<aside data-css-sidebar="sidebar">
<form data-js-form="filter">
<h3>Filter <?php echo ucfirst($type); ?></h3>
<fieldset style='display:none'>
<label name='product-category' for="product-category"><input id='product-category' name='product-category' value=<?php echo $cat_id; ?>></label>
</fieldset>
<fieldset>
<label for="product-prices">Prices</label>
<?php
$prices = get_terms(array(
'taxonomy' => 'price',
'hide_empty' => false,
));
?>
<select id="product-price" name="product-price">
<option>Search by Price</option>
<?php foreach ($prices as $price) : ?>
<option value="<?= $price->term_id; ?>"><?= $price->name; ?></option>
<?php endforeach; ?>
</select>
</fieldset>
<fieldset>
<label for="product-brand">Brands</label>
<?php
$prices = get_terms(array(
'taxonomy' => 'brands',
'hide_empty' => false,
));
?>
<select id="product-brand" name="product-brand">
<option>Search by Brand</option>
<?php foreach ($prices as $price) :
?>
<option value="<?= $price->term_id; ?>"><?= $price->name; ?></option>
<?php endforeach; ?>
</select>
</fieldset>
<fieldset>
<label>Colors</label>
<?php
$colors = get_terms(array(
'taxonomy' => 'color',
'hide_empty' => false,
));
foreach ($colors as $color) :
?>
<div>
<input type="checkbox" id="<?= $color->slug; ?>" name="product-colors[]" value="<?= $color->term_id; ?>"><label for="<?= $color->slug; ?>"><?= $color->name; ?></label>
</div>
<?php endforeach; ?>
</fieldset>
<fieldset>
<button>Filter</button>
<input type="hidden" name="action" value="filter">
</fieldset>
</form>
</aside>
filter.php:
<?php
add_action('wp_ajax_nopriv_filter', 'filter_ajax');
add_action('wp_ajax_filter', 'filter_ajax');
function filter_ajax()
{
$product_cat = $_POST['product-category'];
$product_color = $_POST['product-colors'];
$product_price = $_POST['product-price'];
$product_brand = $_POST['product-brand'];;
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'cat' => $product_cat,
);
if (isset($product_price) && !empty($product_price)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'price',
'field' => 'term_id',
'terms' => array($product_price)
),
);
}
if (isset($product_brand) && !empty($product_brand)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'brands',
'field' => 'term_id',
'terms' => array($product_brand)
)
);
}
if (isset($product_color) && !empty($product_color)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'color',
'field' => 'term_id',
'terms' => $product_color
)
);
}
$query = new WP_Query($args);
// start the loop
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$no_img = get_field('no_image', 'options')['url'];
?>
<div class="card mx-4 mx-md-2 mx-xl-4">
<div class="card__inner">
<?php if (has_post_thumbnail()) { ?>
<img src="<?php the_post_thumbnail_url(); ?>" alt="" />
<?php } else {
?>
<img src="<?php echo $no_img ?>" alt="no-image" />
<?php } ?>
<h6 class="mt-3"><?php the_title(); ?></h6>
<p>$ <?php echo get_field('price'); ?></p>
</div>
</div>
<?php endwhile;
else : ?>
<div>
<p>No items found</p>
</div><?php
endif; ?>
<?php wp_reset_query();
die();
}
script.js
$.noConflict();
$(document).ready(function () {
//ajax filtering
$(document).on("submit", "[data-js-form=filter]", function (e) {
e.preventDefault();
let data = $(this).serialize();
$.ajax({
url: "/wp-admin/admin-ajax.php",
data: data,
type: "post",
success: function (result) {
console.log(result);
$("#target").html(result);
},
error: function (result) {
console.warn("error :" + result);
},
});
});
})

Try changing to something like the following
if (isset($product_color) && !empty($product_color)) {
$args['tax_query'][] = array(
'taxonomy' => 'color',
'field' => 'term_id',
'terms' => $product_color
);
}
And the same for the others
You might also need to change this (adding the tax_query)
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'cat' => $product_cat,
'tax_query' => ['relation' => 'or']
);

Related

Post filtering in wordpress returns all posts

I have this function to filter posts by fields and taxonomies. The problem is that when I use the taxonomy filter it doesn't work and it returns all the posts without filtering.
I didn't include js files because ajax is working well.
Here's the PHP in functions.php:
function post_filter()
{
$number = $_POST['number'];
$autor = $_POST['autor'];
$args = array(
'post_type' => 'publicaciones',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'numero',
'meta_value'=> $number
);
if( isset( $autor ) )
$args['tax_query'] = array(
array('taxonomy' => 'autors',
'field' => 'id',
'terms' => $autor)
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
//Post content
endwhile;
wp_reset_postdata();
else :
echo 'No results.';
endif;
wp_die();
}
And this is the form:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" class="my-5" id="buscador_numero">
<div class="row">
<div class="col-3">
<?php
if( $terms = get_terms( array( 'taxonomy' => 'autors', 'orderby' => 'name' ) ) ) :
echo '<select name="autor"><option value="">Autor/a</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
?>
</div>
<div class="col-2">
<input type="number" class="w-100 p-2" name="number" placeholder="Número">
</div>
<div class="col-2">
<button id="btn_buscar"><span class="dashicons dashicons-search"></span></button>
</div>
</div>
<input type="hidden" name="action" value="post_filter">
</form>
Thanks in advance :)

Custom taxonomy archive showing all posts

I have a custom post type 'vacatures' with two custom taxonomies 'type' and 'locatie'. I created an archive page to display all the posts, but now when I view the link for all the posts belonging to a specific 'type', it just shows all the posts like the archive page.
How can I solve this, making sure I can still make the filters work?
archive-vacatures.php:
<?php get_header(); ?>
<div class="jobs__banner">
<h1><?php _e( 'VACATURES', 'ago' ) ?></h1>
</div>
<div class="vacatures__archief">
<?php
$types = get_terms([ 'taxonomy' => 'type' ]);
$locations = get_terms([ 'taxonomy' => 'locatie']);
$postsQuery = [
'order' => 'DESC',
'post_type' => 'vacatures',
'posts_per_page' => -1,
'tax_query' => []
];
if(array_key_exists('type_id', $_GET) && $_GET['type_id']) {
$postsQuery['tax_query'][] = [
'taxonomy' => 'type',
'field' => 'term_id',
'terms' => [$_GET['type_id']]
];
}
if(array_key_exists('locatie_id', $_GET) && $_GET['locatie_id']) {
$postsQuery['tax_query'][] = [
'taxonomy' => 'locatie',
'field' => 'term_id',
'terms' => [$_GET['locatie_id']]
];
}
if(array_key_exists('query', $_GET) && $_GET['query']) {
$postsQuery['s'] = $_GET['query'];
}
query_posts($postsQuery); ?>
<form class="vacature__filter">
<input type="text" name="query" placeholder="Search.." />
<select name="type_id">
<option value="">-- <?php _e( 'Alle types', 'ago' ) ?> --</option>
<?php foreach($types as $type) {
$selected = array_key_exists('type_id', $_GET) && $_GET['type_id'] == $type->term_id ? 'selected' : '';
echo "<option value='{$type->term_id}' {$selected}>{$type->name}</option>";
} ?>
</select>
<select name="locatie_id">
<option value="">-- <?php _e( 'Alle locaties', 'ago' ) ?> --</option>
<?php foreach($locations as $locatie) {
$selected = array_key_exists('locatie_id', $_GET) && $_GET['locatie_id'] == $locatie->term_id ? 'selected' : '';
echo "<option value='{$locatie->term_id}' {$selected}>{$locatie->name}</option>";
} ?>
</select>
<button type="submit">Zoek</button>
</form>
<?php if ( have_posts() ) { ?>
<?php
while ( have_posts() ) {
the_post();
?>
<article id="vacature-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="vacature__item" href="<?php the_permalink(); ?>">
<h2 class="vacature__title"><?php the_title(); ?></h2>
<h4 class="vacature__company"><?php echo get_field('vacature_bedrijf') ?> - <?php $terms = get_the_terms( $post->ID , 'type' ); $i = 1; foreach ( $terms as $term ) { $term_link = get_term_link( $term, 'type' ); if( is_wp_error( $term_link ) ) continue; echo $term->name; echo ($i < count($terms))? ", " : ""; $i++; } ?></h4>
<h6 class="vacature__location"><?php $terms = get_the_terms( $post->ID , 'locatie' ); foreach ( $terms as $term ) { echo $term->name; } ?></h6>
</a>
</article>
<?php
}
}
?>

How to get the list of terms with its CPT?

I have a map in my website with dots that I localize the number of office per country on a map picture.
How it works is basically I have CPT as office and I create posts as city name like New York, London etc.
For example, if I have an office in New York, USA, I will create the post as New York and custom category will be country name as the USA. Also, in office CPT, I have custom fields for coordinating the dots on the map as Home_x and Home_Y.
So the outcome with the below code is like:
USA/New York
USA/Chicago
United Kingdom/London
United Kingdom/Bristol
Spain/Barcelona
Spain/Granada
My code for loop is;
<div class="map-wrapper">
<div class="map">
<?php
$terms = get_terms(array(
'taxonomy' => 'office-country',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'office', //
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'office-country',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
$info = get_post_meta(get_the_ID(), '_post_info', true);
$link = get_term_meta($term->term_id, 'link', true);
?>
<a href="<?php echo $link ?>"
class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
style="left: <?php echo $info['home_x']; ?>px; top: <?php echo $info['home_y']; ?>px;"
data-target=".country-popup-<?php echo $term->term_id ?>">
<div class="inner"></div>
<div class="text">
<span class="name"><?php echo $re[0] ?> </span>
<span class="number"><?php echo " / ".$title; ?>
</span>
</div>
</a>
<?php
}
}
endforeach; ?>
</div>
</div>
and my custom field for CPT is;
<tr>
<th>
<label><?php _e('Home X'); ?></label>
</th>
<td>
<input type="text" name="_post_info[home_x]" value="<?php echo $info['home_x'] ?>">
</td>
</tr>
<tr>
<th>
<label><?php _e('Home Y'); ?></label>
</th>
<td>
<input type="text" name="_post_info[home_y]" value="<?php echo $info['home_y'] ?>">
</td>
</tr>
Above code works perfectly for this purpose.
But I want to change the custom taxonomy as office and post as a country name now. Instead of creating multiple posts for the city, creating a country post and adding cities as custom taxonomy is much easier. So I am trying to change below code for a new way.
I have changed the loop code as below, and create a custom field for custom-taxonomy, I also indicate it below too.
New loop;
<div class="map-wrapper">
<div class="map">
<?php
$terms = get_terms(array(
'taxonomy' => 'office-city',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'office-country', //
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'office-city',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
// $info = get_post_meta(get_the_ID(), '_post_info', true);
$link = get_term_meta($term->term_id, 'link', true);
$MapY = get_term_meta($term->term_id, 'home_y', true);
$MapX = get_term_meta($term->term_id, 'home_x', true);
?>
<a href="<?php echo $link ?>"
class="point <?php if ($term->slug === $_GET['country']) echo 'active' ?>"
style="left: <?php echo $MapY ?>px; top: <?php echo $MapX ?>px;"
data-target=".country-popup-<?php echo $term->term_id ?>">
<div class="inner"></div>
<div class="text">
<span class="name"><?php echo $title; ?> </span>
<span class="number"><?php echo " / ".$re[0] ?>
</span>
</div>
</a>
<?php
}
}
endforeach; ?>
</div>
</div>
Custom field for taxonomy;
add_action( 'office-country_edit_form_fields', 'office_country_taxonomy_custom_fields', 10, 2 );
function office_country_taxonomy_custom_fields($tag) {
?>
<tr>
<th>
<label><?php _e('Home X'); ?></label>
</th>
<td>
<input type="text" name="_term_meta[home_x]" value="<?php echo get_term_meta($tag->term_id, 'home_x', true) ?>">
</td>
</tr>
<tr>
<th>
<label><?php _e('Home Y'); ?></label>
</th>
<td>
<input type="text" name="_term_meta[home_y]" value="<?php echo get_term_meta($tag->term_id, 'home_y', true) ?>">
</td>
</tr>
<?php
}
But when I applied this code, it partly works and I have no idea to change the query.
The result;
USA/New York
United Kingdom/London
Spain/Barcelona
So, it does only show posts per taxonomy but I want to show all the cities per country like below;
USA/New York
USA/Chicago
United Kingdom/London
United Kingdom/Bristol
Spain/Barcelona
Spain/Granada
Sorry, it is a long question. I hope you guys can help on this, and I hope you guys understood my broken English.
Your loop is working correctly as i got result what you wanted.
<?php
$terms = get_terms(array(
'taxonomy' => 'office-city',
'hide_empty' => false,
));
foreach ($terms as $term) :
$args = array (
'post_type' => 'office-country', //
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'office-city',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
echo '<p>'. $title . '/' . $term->name . '</p>';
}
}
endforeach; ?>

'key' meta_query not working custom advanced search (WordPress)

Been trying for hours now and have almost got to breaking point. Tried so many different things but can't seem to get my custom search bar to query results by a pre-defined select price range. I am using Advanced Custom Fields to add the meta tag 'investmentprice' to my custom post type called 'investments'. I feel like it's to do with WordPress not picking up the correct 'key' in my meta_query, even though I have changed this numerous times now.
Here's the code for the config bar:
<div class="config_bar cf">
<form method="get" role="search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="s" value="">
<input type="hidden" name="post_type" value="investments" />
<div class="col">
<?php
$taxonomy = 'type';
$args = array( 'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
);
$tax_terms = get_terms($taxonomy, $args);
?>
<span class="label_inner">Investment Type</span>
<select name="type" id="type" class="postform standard">
<option value="" selected="selected">All Investment Types </option>
<?php if($tax_terms): ?>
<?php foreach ($tax_terms as $tax_term): ?>
<?php $title = $tax_term->name;
?>
<option value="<?php echo $tax_term->slug; ?>"><?php echo $title; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div><!-- col -->
<div class="col">
<span class="label_inner"><?php _e('Select Country','opencloud');?></span>
<select class="postform standard country " name="country" id="country">
<option value=""><?php _e('All Countries','opencloud');?></option>
<?php
// Display only parents here .
$terms = get_terms( array(
// Put your taxonomy name here.
'taxonomy' => 'location',
'parent' => 0,
'hide_empty' => false
) );
foreach ($terms as $term){?>
<!-- We are going to send value for $_POST and data-makeId's TERM_ID for ajax request -->
<option value="<?php echo $term->slug;?>" data-countryId="<?php echo $term->term_id ?>"><?php echo $term->name;?></option>
<?php
wp_reset_query(); // if you're not in your main loop! otherwise you can skip this
} ?>
</select>
</div><!-- col -->
<script type="text/javascript">
$( document ).ready(function() {
$('#country').change(function(){
var $mainCat= $(this).find(':selected').attr('data-countryId');
if ($mainCat != '0' ){
// call ajax
$("#city").empty();
$.ajax
(
{
url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
type:'POST',
data:'action=get_city_lists_ajax&main_catid=' + $mainCat,
beforeSend:function()
{
},
success:function(results)
{
$("#loading_bar").hide();
$("#city").removeAttr("disabled").trigger('change.select2');
$("#city").append(results).trigger('change.select2');
}
}
);
}
});
});
</script>
<div class="col">
<span class="label_inner"><?php _e('Select City','opencloud');?></span>
<select class="postform standard city " name="city" id="city" disabled>
<option value="<?php echo $term->slug;?>"><?php _e('All Cities','opencloud');?></option>
</select>
</div><!-- col -->
<div class="col">
<span class="label_inner">Select Price</span>
<select class="postform standard price" name="price" id="price">
<option value="">All Prices</option>
<option value="500-1000">£500-£1000</option>
<option value="1000-1500">£1000-£1500</option>
<option value="1500-2000">£1500-£2000</option>
<option value="2000-5000">£1500-£2000</option>
<option value="5000-10000">£5000-£10,000</option>
</select>
</div><!-- col -->
<div class="col">
<button class="search_submit" type="submit">Search</button>
</div><!-- col -->
</form>
And here's my advanced search query function:
function advanced_search_query( $query ) {
if ( isset( $_REQUEST['search'] ) && $_REQUEST['search'] == 'advanced' && !is_admin() && $query->is_search && $query->is_main_query() ) {
// limit query for custom post type
$query->set( 'post_type', 'investments' );
// Get query strings from URL and store the min a variable
$_type = $_GET['type'] != '' ? $_GET['type'] : '';
$_country = $_GET['country'] != '' ? $_GET['country'] : '';
$_city = $_GET['city'] != '' ? $_GET['city'] : '';
$_price = $_GET['price'] != '' ? $_GET['price'] : '';
if( $_price != '' ) {
$metaquery = array(
array(
'key' => 'investmentprice',
'terms' => $_price,
'compare' => 'BETWEEN'
)
);
$query->set( 'meta_query', $metaquery );
}
// if type is not empty limit the taxonomy to the specified
if( $_type != '' ) {
$taxquery = array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => $_type,
'operator'=> 'IN'
)
);
$query->set( 'tax_query', $taxquery );
}
// if country is not empty limit the taxonomy to the specified
if( $_country != '' ) {
$taxquery = array(
array(
'taxonomy' => 'country',
'field' => 'slug',
'terms' => $_country,
'operator'=> 'IN'
)
);
$query->set( 'tax_query', $taxquery );
}
// if city is not empty limit the taxonomy to the specified
if( $_city != '' ) {
$taxquery = array(
array(
'taxonomy' => 'city',
'field' => 'slug',
'terms' => $_city,
'operator'=> 'IN'
)
);
$query->set( 'tax_query', $taxquery );
}
return; // always return
}
}
Also, here's a screenshot of the Advanced Custom Field:
Unless there are any other issues, this should be a small fix. You are using the array key 'terms' instead of 'value' in your meta query. According to the WordPress Codex, a nested array of args for a meta query should use value, like this:
$metaquery = array(
array(
'key' => 'investmentprice',
'value' => $_price,
'compare' => 'BETWEEN'
)
);
$query->set( 'meta_query', $metaquery );
Hope that helps..

Search custom post type by year

I've created a more advanced search for the standard Wordpress search. To go along with the keyword text input I've added two drop downs that are searching my custom taxonomies.
This is working great.
The only dropdown I've yet to create the functionality for is the year dropdown. I'd like to be able to filter the results to show posts within that specific year.
How can I search through all the posts and only output the custom posts that were published for that year?
It's important that the posts are searched for by their publish date (ie. no custom fields!).
<form action="<?php echo home_url( '/' ); ?>" method="get" id="advanced-search">
<div id="search-nudge">
<h1 id="search-title">Search Policy's</h1>
<p id="search-strap">Search by some or all of the following criteria</p>
<div class="clear"></div>
<div id="search-left">
<p class="search-label">Keyword</p>
<input type="text" name="s" id="s" class="keyword" value="<?php the_search_query(); ?>" />
<input type="hidden" value="policies" name="post_type" id="post_type" /><br /><br />
</div>
<div id="search-right">
<p class="search-label">Year of publication</p>
<select class="work-list search-dropdown" name="year">
<option value="">All</option>
</select>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="search-element">
<?php // List all event subjects
$subjectList = get_terms('topics', array( 'taxonomy' => 'topics' ));
if(!empty($subjectList)) { ?>
<p class="search-label">Category</p>
<select class="work-list search-dropdown" name="subject">
<option value="">All</option>
<?php foreach($subjectList as $subject){
if(isset($_GET['subject'])) {
$selected = ($subject->slug == $_GET['subject']) ? ' selected="selected"' : '';
}
echo '<option value="' . $subject->slug . '"'. $selected.'> ' . $subject->name . '</option>';
}
echo '</select><br /><br />';
} ?>
</div>
<div class="search-element">
<?php // list all event types
$document = get_terms('document_type', array( 'taxonomy' => 'document_type' ));
if(!empty($subjectList)) { ?>
<p class="search-label">Document Type</p>
<select class="work-list search-dropdown search-dropdown-right" name="type">
<option value="">All</option>
<?php foreach($document as $type){
if(isset($_GET['type'])) {
$selected = ($type->slug == $_GET['type']) ? ' selected="selected"' : '';
}
echo '<option value="' . $type->slug . '"'. $selected.'> ' . $type->name . '</option>';
}
echo '</select><br /><br />';
} ?>
</div>
<input type="radio" name="sort" class="search-input" value="date" checked> <p class="search-label radio radio-nudge">Order by date</p>
<input type="radio" name="sort" class="search-input" value="title"> <p class="search-label radio">Order by title</p>
<input type="submit" id="searchsubmit2" class="search-submit" value="Search" />
<div class="clear"></div>
</div>
</form>
<?php if(isset($_GET['sort']) && $_GET['sort'] === "date") {
$sort = $_GET['sort'];
$order = "DESC";
} else {
$sort = $_GET['sort'];
$order = "ASC";
}
// default args to search by custom post type and search term
$args = array(
'post_type' => 'policies',
'posts_per_page' => 10,
'orderby' => $sort,
'order' => $order,
's' => $s
);
// add the subject to args if there is one
if(isset($_GET['subject']) && $_GET['subject'] != "") {
$args['tax_query'][1] = array(
'taxonomy' => 'topics',
'field' => 'slug',
'terms' => array($_GET['subject'])
);
}
// add the subject to args if there is one
if(isset($_GET['type']) && $_GET['type'] != "") {
$args['tax_query'][1] = array(
'taxonomy' => 'document_type',
'field' => 'slug',
'terms' => array($_GET['type'])
);
}
// Run the query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
} else {
echo "No results";
}
/* Restore original Post Data */
wp_reset_postdata(); ?>
Update:
I've tried the following but no luck displaying any posts.
if(isset($_GET['year']) && $_GET['year'] != "") {
$args = array(
'date_query' => array(
array(
'year' => array($_GET['year'])
),
),
);
}
Solved with:
if(isset($_GET['year']) && $_GET['year'] != "") {
$args = array(
'post_type' => 'policies',
'posts_per_page' => 10,
'orderby' => $sort,
'order' => $order,
's' => $s,
'date_query' => array(
array(
'year' => ($_GET['year'])
),
),
);
} else {
// default args to search by custom post type and search term
$args = array(
'post_type' => 'policies',
'posts_per_page' => 10,
'orderby' => $sort,
'order' => $order,
's' => $s
);
}

Categories