简体   繁体   中英

WP_Query retuns error if term is blank Wordpress

I am trying to create a filter using WP_Query() in which I am sending term id by POST method via AJAX, These term ids are from multiple taxonomies:

  1. Collateral_services
  2. Collateral_Technology
  3. Collateral_Industry

I've created a WP_Query which works fine if $ser , $tech , $ind are not blank .

$cat_ids = $_POST['category'];
    $ser = $_POST['services'];
    $tech = $_POST['technology'];
    $ind = $_POST['industry'];
    $paged = $_POST['page']+1;

        $args = array(
            'post_type' => 'page',
            'category__in' => $cat_ids,
            'post_status' => 'publish',
            'orderby' => 'DESC',
            'paged' => $paged,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'Collateral_services',
                    'field'    => 'id',
                    'terms'    => $ser,
                ),
                array(
                    'taxonomy' => 'Collateral_Technology',
                    'field'    => 'id',
                    'terms'    => $tech,
                ),
                array(
                    'taxonomy' => 'Collateral_Industry',
                    'field'    => 'id',
                    'terms'    => $ind,
                ),
            ),
    
        );
        $new = new WP_Query($args);

But what I want is to create dynamic kind of query, query should work if user is choosing only Collateral_services or Collateral_Technology or Collateral_Industry, or by combination or any two or all together,

Can someone help to make it Dynamic?

Any Help Appreciated.

// Assuming cat ids is commma seperated so used santize_text_field.
$cat_ids = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '';

// Assuming all these are single number.
$service    = isset( $_POST['services'] ) ? absint( wp_unslash( $_POST['services'] ) ) : '';
$technology = isset( $_POST['technology'] ) ? absint( wp_unslash( $_POST['technology'] ) ) : '';
$industry   = isset( $_POST['industry'] ) ? absint( wp_unslash( $_POST['industry'] ) ) : '';
$paged      = isset( $_POST['page'] ) ? ( absint( wp_unslash( $_POST['page'] ) ) + 1 ) : 1;

$query_args = array(
    'post_type'   => 'page',
    'post_status' => 'publish',
    'orderby'     => 'DESC',
    'paged'       => $paged,
);

$tax_query = array();

if ( ! empty( $cat_ids ) ) {
    $query_args['category__in'] = $cat_ids;
}

if ( ! empty( $service ) ) {
    $tax_query[] = array(
        'taxonomy' => 'Collateral_services',
        'field'    => 'id',
        'terms'    => $service,
    );
}

if ( ! empty( $technology ) ) {
    $tax_query[] = array(
        'taxonomy' => 'Collateral_Technology',
        'field'    => 'id',
        'terms'    => $technology,
    );
}
if ( ! empty( $industry ) ) {
    $tax_query[] = array(
        'taxonomy' => 'Collateral_Industry',
        'field'    => 'id',
        'terms'    => $industry,
    );
}

if ( ! empty( $tax_query ) ) {
    $tax_query['relation']   = 'AND';
    $query_args['tax_query'] = $tax_query;
}

$new_query = new WP_Query( $query_args );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM