Skip to content

Nested loops might have side effects #8

@tangrufus

Description

@tangrufus

See https://neliosoftware.com/blog/the-problems-of-using-nested-loops-in-wordpress/

public static function flGetLocations($postType): array
{
$locations = [];
$locQuery = new WP_Query([
'post_type' => $postType,
'meta_key' => 'location',
'meta_query' => [
[
'key' => 'location',
'value' => '',
'compare' => '!=',
],
],
]);
if ($locQuery->have_posts()) {
while ($locQuery->have_posts()) {
$locQuery->the_post();
$location = get_field('location', get_the_ID());
if (! array_key_exists($location, $locations)) {
$locations[esc_attr($location)] = $location;
}
}
}
wp_reset_postdata();
return $locations;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions