Logic issue | WordPress.org

[ad_1]

This is the PHP code:


	if($_GET["yearSearch"] != ""){
		array_push($filter, array('key' => 'year', 'type' => 'NUMERIC', 'value' => explode(",", $_GET["yearSearch"]), 'compare' => '='));
	}

$arg = array('posts_per_page' => $posts_per_page,
		'paged' => $paged,
		'post_type' => 'inventory',
		'meta_query' => array('relation' => 'AND', $filter),
		'orderby' => 'ASC');

	$posts = get_posts($arg);

Complementing: if I just add one year like 2019 it returns cars from 2019.. the issue is when I have more than one… 2023,2022,2019…

The problem could be the way of comparison at the filter. You currently write

'value' => explode(",", $_GET["yearSearch"]), 'compare' => '='

Use:

'value' => $_GET["yearSearch"], 'compare' => 'IN'

Background: if you use an array as a list here, you can use “IN” to consider all values contained in it. See: https://wordpress.stackexchange.com/questions/55354/how-can-i-create-a-meta-query-with-an-array-as-meta-field

If it still doesn’t work, debug your code carefully. Check what you pass to get_posts() as an array and compare that with the specifications of WP_Query especially regarding meta_queries:

If you still need help it would also be interesting to see what exactly is in $GET[‘yearSearch’].

@threadi bro this is GOLD, it works perfectly! Thank you so much, I appreciate your help!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer