Wordpress get only posts with matching title not content - wordpress

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.

Related

WooCommerce - Filtering related products by product attribute and category

I'm looking to adapt the related products so that it returns products that match the colour and category of the current product.
I can get one or the other working, but not both.
Please see code below:
$attribute = 'pa_colour';
$term_slugs = wp_get_post_terms( $product_id, $attribute, ['fields' => 'slugs'] );
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
if ( empty($term_slugs) )
return $related_posts;
$new_relations = get_posts( array(
'post_type' => 'product',
'posts_per_page' => 4,
'post__not_in' => array( $product_id ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $attribute,
'field' => 'slug',
'terms' => $term_slugs,
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_cat_id,
),
),
'fields' => 'ids',
'orderby' => 'rand',
) );
If anyone can point me in the right direction, that would be great.
Thanks
After trying many things, it turns out it's as simple as dropping the product category into the first part of the query like so:
'post_type' => 'product',
'product_cat' => 'carpet',
'posts_per_page' => 4,
'post__not_in' => array( $product_id ),
'tax_query' => array(

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']
)
)
);

Wordpress query adding 0=1 in wp_query and result dissappear

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

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();
}
}

Resources