$args['meta_query'] = array( 'relation'=>'AND' );
if( isset( $_POST['datafilter_price'] ) && !empty( $_POST['datafilter_price'] ) ){
/*
$args['orderby'] = 'meta_value';
$args['meta_key'] = 'tovar_price';
switch ($_POST['datafilter_price']) {
case 'price_up':
$args['order'] = 'ASC';
break;
case 'price_down':
$args['order'] = 'DESC';
break;
default:
$args['order'] = 'ASC';
}
*/
switch ($_POST['datafilter_price']) {
case 'price_up':
$_order = 'ASC';
break;
case 'price_down':
$_order = 'DESC';
break;
default:
$_order = 'ASC';
}
$args['meta_query']['tovar_price'] = array(
'key' => 'tovar_price',
'type' => 'NUMERIC',
'compare' => 'EXISTS'
);
$args['orderby'] = array(
'tovar_price' => $_order,
);
}
Array
(
[post_type] => products
[numberposts] => -1
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => catalog
[field] => id
[terms] => 294
)
)
[post__not_in] => Array
(
[0] => 14703
)
[meta_query] => Array
(
[relation] => AND
)
[orderby] => meta_value
[meta_key] => tovar_price
[order] => DESC
)
Array
(
[post_type] => products
[numberposts] => -1
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => catalog
[field] => id
[terms] => 294
)
)
[post__not_in] => Array
(
[0] => 14703
)
[meta_query] => Array
(
[relation] => AND
[tovar_price_num] => Array
(
[key] => tovar_price
[type] => NUMERIC
[compare] => EXISTS
)
)
[orderby] => Array
(
[tovar_price_num] => DESC
)
)
function true_filter_function(){
$taxonomy = sanitize_post($_POST['taxonomy'], 'db');
$term_id = sanitize_post($_POST['term_id'], 'db');
$current_id = sanitize_post($_POST['current_id'], 'db');
$prod_name_hover = 'Подробнее';
$args = array(
'post_type' => 'products',
'numberposts' => -1,
'tax_query'=> array(
array(
'taxonomy'=> $taxonomy,
'field'=> 'id',
'terms'=>$term_id
)
),
'post__not_in' => array($current_id)
);
if( isset( $_POST['datafilter_model'] ) || isset( $_POST['datafilter_labels'] ) || isset( $_POST['datafilter_diagonal'] ))
$args['meta_query'] = array( 'relation'=>'AND' );
if( isset( $_POST['datafilter_price'] ) && !empty( $_POST['datafilter_price'] ) ){
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'tovar_price';
switch ($_POST['datafilter_price']) {
case 'price_up':
$args['order'] = 'ASC';
break;
case 'price_down':
$args['order'] = 'DESC';
break;
default:
$args['order'] = 'DESC';
}
/*
switch ($_POST['datafilter_price']) {
case 'price_up':
$_order = 'ASC';
break;
case 'price_down':
$_order = 'DESC';
break;
default:
$_order = 'DESC';
}
$args['meta_query']['tovar_price'] = array(
'key' => 'tovar_price',
'type' => 'NUMERIC',
'compare' => 'EXISTS'
);
$args['orderby'] = array(
'tovar_price' => $_order,
);*/
}
// условие 1: label
if( isset( $_POST['datafilter_labels'] ) && !empty( $_POST['datafilter_labels'] ) ){
$_pl = explode(',', sanitize_post( $_POST['datafilter_labels'], 'db'));
$_arr_labels = array( 'relation'=>'OR' );
foreach ($_pl as $_item){
$_arr_labels[] = array(
'key' => 'tovar_labels',
'value' => $_item,
'compare' => 'LIKE'
);
}
$args['meta_query'][] = $_arr_labels;
}
// условие 2: diagonal
if( isset( $_POST['datafilter_diagonal'] ) && !empty( $_POST['datafilter_diagonal'] ) ){
$_pd = explode(',', sanitize_post( $_POST['datafilter_diagonal'], 'db'));
$args['meta_query'][] = array(
'key' => 'tovar_diagonal',
'value' => $_pd,
'compare' => 'IN'
);
}
// условие 3: модель
if( isset( $_POST['datafilter_model'] ) && !empty( $_POST['datafilter_model'] ) ){
$args['meta_query'][] = array(
'key' => 'tovar_name_model',
'value' => sanitize_post( $_POST['datafilter_model'], 'db'),
'compare' => 'LIKE'
);
}
//echo print_r($args, true); die();
$_query = new WP_Query( $args );
if ( $_query->have_posts() ){ ?>
<?php while ( $_query->have_posts() ){ $_query->the_post(); ?>
<?php
global $post;
$_prod_image = get_field('tovar_cat_image',$post->ID);
$_tovar_name = get_field('tovar_name',$post->ID);
$_tovar_name_model = get_field('tovar_name_model');
$_tovar_diagonal = get_field('tovar_diagonal');
$_tovar_labels = get_field('tovar_labels',$post->ID);
$_tovar_price = get_field('tovar_price');
if($_tovar_name_model) $_tovar_name .= ' '.$_tovar_name_model;
?>
<div class="col-lg-3 col-md-4 col-sm-6 col-6 prod_item" id="<?=$post->ID?>">
<a href="<?=get_the_permalink()?>" class="prod_item_link">
<?php
if($_tovar_labels){
?>
<span class="prod_labels">
<?php
foreach ($_tovar_labels as $_label_item){
$_prod_label_image = '';
$_prod_label_image = get_field('prod_label_image_'.$_label_item['value'], 'options');
?>
<span class="prod_labels_item">
<img src="<?=$_prod_label_image?>"/>
</span>
<?php
}
?>
</span>
<?php
}
?>
<img src="<?=$_prod_image?>" class="prod_item_image img-responsive img-fluid"/>
<span class="prod_item_name"><?=$_tovar_name?> <?=$_tovar_diagonal?> <?=$_tovar_price?></span>
<span class="prod_item_name_hover "><?=$prod_name_hover?></span>
</a>
</div>
<?php }?>
<? wp_reset_postdata(); ?>
<?php }else{
echo '<div class="col-lg-12">Товаров не найдено</div>';
}
die();
}