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
6
  • I don't entirely follow--can you not use strpos? php.net/manual/en/function.strpos.php Commented Nov 21, 2018 at 0:43
  • @ggorlen why strops? I have an entire string, I need to check if $myterm is in string Commented Nov 21, 2018 at 0:44
  • @rob.m That's exactly what strpos does, check if a string is in a string. Commented Nov 21, 2018 at 0:45
  • Possible duplicate of How do I check if a string contains a specific word? Commented Nov 21, 2018 at 0:47
  • @ggorlen question updated Commented Nov 21, 2018 at 0:52

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

I still get array(0) { } even tho the strings are as per the bottom of my question
try to echo $id to be sure $id is not blank.
if I place echo $id before in_array, I get 127431 127429 127427 array(0) { }
this isn't working $custom_field = explode(" ", $custom_field);
explode should work. check your $custom_field >> echo $coustom_field; before explode
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.