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

IMPD-INC
  • 27
  • 13

2 Answers2

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
                        )

                )

        )

)
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51
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.

Beso Kakulia
  • 556
  • 4
  • 13