1

I want to work with a loop inside the search arguments, like below

$community = array("eleven_polls", "activities_coordinator", "attractively_priced_onsite_laundry_care_centers");
$community_count = count($community);

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'apartment',
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'relation' => 'AND',
            for ($i=0; $i < $community_count ; $i++) { 
                array(
                    'key'       => 'community',
                    'value'     => $_GET['community'][$i],
                    'compare'   => 'LIKE'
                ),
            }
        )
    )
);

but it is showing syntax error on the line for loop, how to I run for loop inside an array

4
  • You can't execute loop inside array Commented Mar 5, 2018 at 5:45
  • but how to achieve this which I want, you have any idea? @Mr.Developer Commented Mar 5, 2018 at 5:46
  • Please check this: stackoverflow.com/a/14446234/1485183 Commented Mar 5, 2018 at 6:09
  • You should remove comma (,) after array close. Commented Mar 5, 2018 at 6:11

2 Answers 2

2

Don't loop inside the array. do like as below.

$community = array("eleven_polls", "activities_coordinator", "attractively_priced_onsite_laundry_care_centers");
$community_count = count($community);

$args = array(
    'numberposts'   => -1,
    'post_type'     => 'apartment',
    'meta_query'    => array(
    'relation'      => 'AND',
     array(
            'relation' => 'AND',

        )
    )
);


for ($i=0; $i < $community_count ; $i++) { 
               $args = array(
                    'key'       => 'community',
                    'value'     => $_GET['community'][$i],
                    'compare'   => 'LIKE'
                );
            }

Hope, It will work as you are looking for.

echo '<pre>';
print_r($args);
echo '</pre>';

Array
(
    [numberposts] => -1
    [post_type] => apartment
    [meta_query] => Array
        (
            [relation] => AND
            [0] => Array
                (
                    [relation] => AND
                    [0] => Array
                        (
                            [key] => community
                            [value] => Array
                                (
                                    [0] => 0
                                )

                            [compare] => LIKE
                        )

                    [1] => Array
                        (
                            [key] => community
                            [value] => Array
                                (
                                    [0] => 1
                                )

                            [compare] => LIKE
                        )

                    [2] => Array
                        (
                            [key] => community
                            [value] => Array
                                (
                                    [0] => 2
                                )

                            [compare] => LIKE
                        )

                )

        )

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

Comments

1
for ($i=0; $i < $community_count ; $i++) { 
           $array =  array(
                    'key'       => 'community',
                    'value'     => $_GET['community'][$i],
                    'compare'   => 'LIKE'
                ),
            }


$args = array(
        'numberposts'   => -1,
        'post_type'     => 'apartment',
        'meta_query'    => array(
            'relation'      => 'AND',
            array(
                'relation' => 'AND',
                 $array

            )
        )
    );

I do not understand what do you trying to achieve. But try this way.

Comments

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.