Ajax filter for wordpress - wordpress

after a lot of research i came across this: https://rudrastyh.com/wordpress/ajax-post-filters.html
But I can only get it to work when there are two options selected.
I'll give you some context:
I have a CPT named 'Contratistas' that has two custom taxonomies 'especialidad' and 'industria', they both have two terms each 'especialidad' -> 'tecnologia' and 'auditoria'; 'industria' -> 'cultivo' and 'depocito'
here is my function:
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
// for taxonomies / categories
if( isset( $_POST['filtroEspecialidad'] ) && isset ($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
//'relation' => 'AND',
array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
),
array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
),
);
} elseif( !isset($_POST['filtroEspecialidad'] ) && isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
);
} elseif( isset( $_POST['filtroEspecialidad'] ) && !isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
);
}
It works if you select something from both taxonomies, but when one is empty it says 'there are no post'
as a bonus I will like to show all the post before filtering.
I hope somebody can help me! Thanks! I'm fairly new to Wordpress
EDIT! Here is my js and my form, i'm at a loss here I can't figure out what's wrong :(
jQuery(function($){
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('.filtrar').text('Procesando...'); // changing the button label
},
success:function(data){
filter.find('.filtrar').text('Filtrar'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
my php file:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<div class="titulo mb-3">
<h3>Especialidad</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'especialidad', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroEspecialidad"><option value="">Seleccione una especialidad...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<div class="titulo my-3">
<h3>Industrias</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'industria', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroIndustria"><option value="">Seleccione una industria...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button class="my-3 filtrar">Filtrar</button>
Clear
<input type="hidden" name="action" value="myfilter">
</form>

You may want to give the Taxonomy Parameters of WP_Query() another glance. Unfortunately, WordPress is a little loose with it's id parameter names contextually. I'm not entirely sure your initial "both" is working like you intend either, because 'field' => 'id' is actually invalid.
From the docs:
field (string) – Select taxonomy term by. Possible values are term_id, name, slug or term_taxonomy_id. Default value is term_id.
id isn't actually a valid option. If you're just using the term_id, you should be able to omit that. You can also just programatically add the tax_query array arguments based on if that filter is set, instead of checking for "both set, this set/that unset, this unset, that set", something like this perhaps?
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
if( isset($_POST['filtroEspecialidad']) || isset($_POST['filtroIndustria']) ){
$args['tax_query'] = array();
if( isset($_POST['filtroEspecialidad']) ){
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'terms' => $_POST['filtroEspecialidad']
);
}
if( isset($_POST['filtroIndustria']) ){
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'terms' => $_POST['filtroIndustria']
);
}
if( count($args['tax_query']) > 1 ){
$args['tax_query']['relation'] = 'AND';
}
}
// Run WP_Query with new $args, etc.
}
I'm not sure if the $_POST values are arrays or single numbers, but you may want to validate those with array_map and/or absint if you're using user-supplied input, but if they're just IDs, the above should work for now.

Related

wordpress get all post from custom post type that date field is bigger than today

I don't understand why I get all posts and not only the one I want.
I need all posts of the current user that log in and that have the 'date_end' field (acf) is bigger than today.
with this code I get all the post of the user it's like it doesn't read the second array of the conditions :(
this is my code:
$user_id = get_current_user_id();
$today = date("m.d.Y");
$meta_query_args = array(
array(
'key' => 'user_id',
'value' => $user_id,
),
array(
'key' => 'date_end',
'value' => $today,
'compare' => '>'
),
);
$args = [
'posts_per_page' => -1,
'post_type' => 'trip',
'meta_query' => $meta_query_args
];
$TripQuery = get_posts( $args );
if( ! empty( $TripQuery ) ){
foreach( $TripQuery as $TripPost ){
echo '<div name="CurrentTripList">';
echo '<a href="' . $TripPost->ID . '" >' . the_field('date_end',$TripPost->ID) . '</a>';
echo '</div>';
}
}
can some one please help me. I don't understand what I'm doing wrong :(

How do I filter my custom post types by taxonomy with checkboxes?

I am trying to filter my posts from a custom post type called 'courses'. I've created a custom taxonomy called 'course_types' which has 2 terms; 'Bundled Courses' and 'Single Courses'. I have a form with checkboxes, with which I would like to filter the custom posts, and have used a variety of combinations of tax_query operators ("AND, OR, NOT IN, etc"), but I am unabke to acheieve the expected results. Any help on this would be greatly appreciated. Thanks guys.
The Query
<?php
$args =
array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_types',
'field' => 'term_id',
'terms' => $_GET['course_types'],
),
'relation' => 'AND',
),
)
?>
Form with checkboxes
<form method="GET">
<?php $terms = get_terms( array(
'taxonomy' => 'course_types',
'hide_empty' => false,
)
);
foreach($terms as $term) {
echo '<label><input type="checkbox" name="course_types[]" value="' . $term->name
.'">' . $term->name . '</label>';
} ?>
<input type="submit" value="Filter">
</form>
<?php
The Results
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', 'courses' );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Turns out that the issue was that form was returning a name ('Single Courses' and 'Bundled Courses'), bu the query was expecting a term_id.
Corrected code
<form method="GET">
<?php $terms = get_terms( array(
'taxonomy' => 'course_types',
'hide_empty' => false,
) );
foreach($terms as $term) {
echo '<label><input type="checkbox" name="course_types[]" value="'
. $term->term_id .'">' . $term->name . '</label>';
} ?>
<input type="submit" value="Filter">
</form>
The query must contain a conditional if not filtered. I used a ternary.
<?php
$terms = get_terms( 'course_types' );
$term_ids = wp_list_pluck( $terms, 'term_id' );
$args =
array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_types',
'field' => 'term_id',
'terms' => $_GET ? $_GET['course_types'] : $term_ids,
),
'relation' => 'AND',
),
);
?>

How do you include custom field in Shortcode output?

with help from another developer, I have created the following for a custom shortcode:
add_shortcode( 'children' , 'display_custom_post_type_v1' );
function display_custom_post_type_v1($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'operator' => 'AND',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}
So far this works really well for my application, but there is one additional item I would like to include. These custom post types include meta boxes (custom fields) and I would like to include this in the output. Typically I use something like:
<?php echo get_post_meta( $post->ID, '_cd_birthdate', true ); ?>
But I can't figure out how to include it in the output of the shortcode. I've tried this line, but only the div area shows up, but no content:
<div>' . get_post_meta( $post->ID, '_cd_birthdate', true ) . '</div>
Any suggestions would be greatly appreciated.
I think '$post->ID,' only works when in the WP loop. Try 'get_the_ID()' instead.
Like...
<div>' . get_post_meta( get_the_ID(), '_cd_birthdate', true ) . '</div>
See this

Dynamic call of shortcode with parameters

I have this function:
function test_q($atts){
$args = shortcode_atts(array(
'post_type' => 'product',
'columns' => 4,
'posts_per_page' => 12,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'mugs' ),
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => array( 'football' ),
),
),
), $atts);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
} else {
echo __( 'No products found' );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode('testasd', 'test_q');
I want to pass dynamic parameters to 'mugs' and 'football' with the shortcode. I am filtering products by category and tag. This function is working fine but I want to pass those two parameters through shortcode to make it dynamic.
I need to call this shortcode each time with different 'mugs' and 'football' term. For example 't-shirts' and 'basketball'. How will i do that?
You can do it easily via extracting the shortcode and passing the values via $atts from the shortcode.
After your arguments , you can pass it like the following ,
if($args['tax_query']['taxonomy'] == 'product_cat'){
$args['tax_query']['terms'] = $atts['terms_cat'];
}
if($args['tax_query']['taxonomy'] == 'product_tag'){
$args['tax_query']['terms'] = $atts['terms_tag'];
}
Then add 2 parameters in the shortcode,
[testasd terms_cat='t-shirts' terms_tag='basketball']

Custom post type Wordpress query by category

I have the following query which outputs a list of categories for my custom post type called STORIES.
<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div> ';
}
?>
This outputs a list of links for my categories and works great.
My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.
So my query lists the categories...
- Apples
- Oranges
- Bananas
If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?
Any ideas? Can't get any solution to work.
I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?
<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo '<p>'.get_the_title().'</p>';
endwhile;
}
}
?>
you can create custom taxonomy template for custom post : LINK
Hope this help:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$query = new WP_Query( $args );
Class Reference/WP Query

Resources