I have a string in a custom field usp-custom-60
:
Via Prospero Lavarello, 2, 16142 Genova GE, Italia
Then I get a value from GET
$myTerm = $_GET['cityName'];
And now $myterm
is genova
And I have a loop where I am first trying to split the whole string and convert it into an array, then I am asking if a term is in array:
$catIds = array();
while {
$query->the_post();
$id = get_the_ID();
$custom_field = usp_get_meta(false, 'usp-custom-60');
$custom_field = explode(" ", $custom_field);
if (in_array($myTerm, $custom_field, true)) {
array_push($catIds, $id);
var_dump($catIds);
}
But I get no results. If I do var_dump($custom_field)
I get;
array(8) { [0]=> string(3) "Via" [1]=> string(8) "Prospero" [2]=> string(10) "Lavarello," [3]=> string(2) "2," [4]=> string(5) "16142" [5]=> string(6) "Genova" [6]=> string(3) "GE," [7]=> string(6) "Italia" } array(8) { [0]=> string(3) "Via" [1]=> string(8) "Prospero" [2]=> string(10) "Lavarello," [3]=> string(2) "2," [4]=> string(5) "16142" [5]=> string(6) "Genova" [6]=> string(3) "GE," [7]=> string(6) "Italia" } array(8) { [0]=> string(3) "Via" [1]=> string(9) "Adalberto" [2]=> string(7) "Catena," [3]=> string(2) "4," [4]=> string(5) "20121" [5]=> string(6) "Milano" [6]=> string(3) "MI," [7]=> string(6) "Italia" }
Based on the other answer suggested, I tried the following
$myTerm = $_GET['cityName'];
$catIds = array();
$args = get_posts(
array(
'post_type' => 'post',
'posts_per_page' => -1,
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$id = get_the_ID();
$custom_field = usp_get_meta(false, 'usp-custom-60');
if (strpos($custom_field, $myTerm) !== false) {
array_push($catIds, $id);
}
if(count($catIds) > 0){
$arrayFUllCity = "fullCity";
} else {
$arrayFUllCity = "empty";
}
}
//$catIds = implode( ", ", $catIds );
var_dump($catIds);
}
But I get array(0) { }
even tho the echo $custom_field
gives me 3 results with
Via Prospero Lavarello, 2, 16142 Genova GE, Italia
Via Prospero Lavarello, 2, 16142 Genova GE, Italia
Via Adalberto Catena, 4, 20121 Milano MI, Italia