Wordpress query adding 0=1 in wp_query and result dissappear - wordpress

Wp query adding 0=1 in the query and results disappear, I am trying to adding multiple taxonomies in the query but it is producing 0=1. I am trying to fetch results from multiple taxonomies and in case of all I am passing a blank array to get all results
$category = array('');
$post_regions = array('');
$post_tag = array('');
$post_categories = array('');
if($_POST['pageType'] !== "all"){
$category = array (
'taxonomy' => 'category',
'field' => 'slug',
'operator' => 'IN',
'terms' => explode(',', $_POST['pageType'])
);
}
if($_POST['regions'] !== "default"){
$post_regions = array(
'taxonomy' => 'post_regions',
'field' => 'slug',
'operator' => 'IN',
'terms' => explode(',', $_POST['regions'])
);
}
if($_POST['topics'] !== "All"){
$post_tag = array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => explode(',', $_POST['topics']),
'operator' =>'IN'
);
}
$args = array(
'post_type' => 'post',
'orderby' => 'publish_date',
'posts_per_page' => -1,
'order' => 'DESC',
'post_status' => 'publish',
'date_query' => array(
'relation' => 'OR',
$dates
),
'tax_query' => array(
'relation' => 'AND',
$category,
$post_regions,
$post_tag,
$post_categories,
),
);
$query = new WP_Query( $args );

Define the empty array like this:
array();
If you define a array the way you do:
array('');
then array[0] is set.
Regards Tom

Adding variable exists check outside arguments work for me:
$args = array(
'post_type' => 'post',
'orderby' => 'publish_date',
'posts_per_page' => -1,
'order' => 'DESC',
'post_status' => 'publish',
'date_query' => array(
'relation' => 'OR',
$dates
),
'tax_query' => array(
'relation' => 'AND'
),
);
if (isset($category))
$args['tax_query'][] = $category;
if (isset($post_regions))
$args['tax_query'][] = $post_regions;
if (isset($post_tag))
$args['tax_query'][] = $post_tag;
if (isset($post_categories))
$args['tax_query'][] = $post_categories;
$query = new WP_Query( $args );

Because 0=1 evaluates to FALSE, and your entire AND clause is discarded, leaving the entire expression to be relied on the value of the OR clause

There is a block of code who design this SQL return.
In the class class-wp-query.php at line 2106
if ( ! $this->is_singular ) {
$this->parse_tax_query( $q );
$clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
$join .= $clauses['join'];
$where .= $clauses['where'];
}
this return 0=1.... for me

Related

Wp_Query with multiple ACF Taxonomies - Categories and Tags

I created a post-filter display where the admin can go to a page add a ACF block and select what category and tags and that will determine what posts will show on the front-end. Everything is working when only categories are selects and when only tags are selected. But when both categories and tags are selected none show and it defaults to showing all posts ( that is what I want to happen if nothing is selected. ). My code is below any help would be great. If you need more info please let me know. Thank you.
$categories = get_field( 'category', false, true );
if ( $categories ) {
if ( ! is_array( $categories ) ) {
$categories = array( $categories );
}
$categories = array_map( 'intval', $categories );
}
// get the ID values of the terms and not term objects.
$tags = get_field( 'tags', false, true );
if ( $tags ) {
if ( ! is_array( $tags ) ) {
$tags = array( $tags );
}
$tags = array_map( 'intval', $tags );
}
$uncat = get_cat_id( 'Uncategorized' );
$excat = get_cat_id( 'Exclude' );
$excatall = get_cat_id( 'Exclude All' );
if ( ! empty( $categories ) && ! empty( $tags ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'tax_query' => array( // phpcs:ignore
'relation' => 'OR',
array (
'relation' => 'AND',
array(
'taxonomy' => 'category',
'category__not_in' => array( $uncat, $excat, $excatall ),
),
array (
'taxonomy' => 'post_tag',
'terms' => $tags,
'operator' => 'IN',
),
),
array(
'taxonomy' => 'category',
'terms' => $categories,
'operator' => 'IN',
),
),
);
} elseif ( ! empty( $tags ) && empty( $categories ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
'tax_query' => array( // phpcs:ignore
array(
'taxonomy' => 'post_tag',
'terms' => $tags,
),
),
);
} elseif ( ! empty( $categories ) && empty( $tags ) ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
'tax_query' => array( // phpcs:ignore
array(
'taxonomy' => 'category',
'terms' => $categories,
),
),
);
} else {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( $uncat, $excat, $excatall ),
);
}
Got everything to work by adding this in the first section.
$tax_query[] = array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories,
),
array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tags,
),
);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => ASC,
'orderby' => 'date',
'category__not_in' => array( 10,11,12 ),
'tax_query' => $tax_query, // phpcs:ignore
);

Get Products in Category WordPress

I'm trying to pull all products located within a certain category slug.
I've tried the following, however both return every product in the store.
$products = wc_get_products( array( 'category' => array( 'Sony' ) ));
and
$productlist = wp_query(array( 'post_type' => 'product', 'product_cat' => 'Sony'));
Any pointers would be much appreciated.
Try this code
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => 12,// category ID
),
),
);
Try This:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'Sony', 'orderby' => 'rand' );
$data = new WP_Query( $args );
print_r($data);
Try with this and replace ENTER_CATEGORY with slug name or use simple 'terms' => 'slug/categoryname'
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);

show posts with And relation in categories at WordPress

I want to show posts which have categories (a,b and c) with And Relation in query.
I wrote the below code but it shows all the posts that have each of categories. How to show those posts that have all 3 categories with And relation?
<?php
$args2 = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 6,
'relation' => 'And',
'category_name'=>'a','b','c',
'paged' => get_query_var('paged'),
'post_parent' => $parent
);
$q = new WP_Query($args2);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}
?>
I added a tax query, now it checks if a post has categorie a, b or parent of b and the location set.
$args2 = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'tax_query' => array(
// If 'a' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'a' ),
'operator' => 'IN'
),
// AND if 'b' OR 'parent_b' is set.
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'b', 'parent_b' ),
'operator' => 'IN'
),
// AND if location is set.
array(
'taxonomy' => 'location',
'operator' => 'EXISTS'
),
)
);
$q = new WP_Query( $args2 );
if( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
echo the_title();
}
}

Wordpress get only posts with matching title not content

Currently with this code below, I get posts whose title or description contains search keyword. How do I change it, so I only get posts whose title contains search keyword.
$search_keyword = esc_attr( $_REQUEST['query'] );
$ordering_args = $woocommerce->query->get_catalog_ordering_args( 'title', 'asc' );
$suggestions = array();
$args = array(
's' => apply_filters( 'yith_wcas_ajax_search_products_search_query', $search_keyword ),
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters( 'yith_wcas_ajax_search_products_posts_per_page', get_option( 'yith_wcas_posts_per_page' ) ),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'search', 'visible' ),
'compare' => 'IN'
)
)
);
if ( isset( $_REQUEST['product_cat'] ) ) {
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_REQUEST['product_cat']
) );
}
$products = get_posts( $args );
I was trying to add one more query but only make it worse.

WordPress - SQL request with WP_Query, use OR between post_type and taxonomy

My issue is pretty simple, I'm trying to get both portfolio posts (from a plugin) and articles that have the "portfolio" category (made myself).
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)
);
$query = new WP_Query($args);
It looks like the relationship between post_type and tax_query is an AND, I'd need an OR, how can I do that?
I tried following the official documentation but I'm stuck. (https://codex.wordpress.org/Class_Reference/WP_Query)
Thanks for the help.
Here is an example with a relation 'OR':
$args = array(
'post_type' => 'portfolio',
'meta_query' => array(
'relation' => 'OR', /* <-- here */
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query = new WP_Query( $args );
I haven't found any solution using only one query. (couldn't change the AND to OR).
So I've made two queries instead:
$query1 = new WP_Query(array('post_type' => 'portfolio'));
$query2 = new WP_Query(array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'portfolio',
)
)));
$query = merge_WP_queries($query1, $query2);
function merge_WP_queries(WP_Query $query1, WP_Query $query2){
$wp_query_returned = new WP_Query();
$wp_query_returned->posts = array_merge( $query1->posts, $query2->posts );
//populate post_count count for the loop to work correctly
$wp_query_returned->post_count = $query1->post_count + $query2->post_count;
return $wp_query_returned;
}
It works correctly, even though it's not what I'd like to use.

Resources