0

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
rob.m
  • 9,843
  • 19
  • 73
  • 162

2 Answers2

1

Your issue (as far as I can see is that you have a case mismatch between $myTerm which is "genova" and the string, which has "Genova" in it. Since in_array is case-sensitive, the check fails. I would use preg_match on the original string to solve this, using \b to check for word boundaries around $myTerm, and the i modifier to make the regex case-insensitive:

$custom_fields = 'Via Prospero Lavarello, 2, 16142 Genova GE, Italia';
$myTerm = 'genova';
echo preg_match("/\b$myTerm\b/i", $custom_fields);

Output:

1 (true)

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
0

In this kind of case using strpos is best way. From my end it's not possible to run your code because of database relationship. but you can try this solution. May be problem in extra spacing.

$catIds = array();

    while  {
        $query->the_post();
        $id = get_the_ID();
        $custom_field = usp_get_meta(false, 'usp-custom-60');
        $custom_field = explode(" ", $custom_field);
        $custom_field = array_map('trim', $custom_field);
        if (in_array($myTerm, $custom_field, true)) {
            array_push($catIds, $id);
            var_dump($catIds);
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Himel Rana
  • 666
  • 5
  • 14