0

I have couple of checkboxes determine the range of min and max people capacity for events. For example: 0 to 100, 100 to 200, and so on. I need to filter them through with a meta query but currently it doesn't seem working.

array (
  'sort_0' => 
  array (
    'key' => '_featured',
    'type' => 'CHAR',
  ),
  'sort_1' => 
  array (
    'key' => '_claimed',
    'type' => 'CHAR',
  ),
  0 => 
  array (
    'key' => '_min_acik_davet_alani',
    'value' => 
    array (
      0 => '',
    ),
    'compare' => 'NOT IN',
  ),
  1 => 
  array (
    'key' => '_max_acik_davet_alani',
    'value' => 
    array (
      0 => '',
    ),
    'compare' => 'NOT IN',
  ),
  2 => 
  array (
    'relation' => 'OR',
    0 => 
    array (
      0 => 
      array (
        'key' => '_min_kapali_davet_alani',
        'value' => 100,
        'type' => 'NUMERIC',
        'compare' => '>=',
      ),
      1 => 
      array (
        'key' => '_max_kapali_davet_alani',
        'value' => 200,
        'type' => 'NUMERIC',
        'compare' => '<=',
      ),
    ),
    1 => 
    array (
      0 => 
      array (
        'key' => '_min_kapali_davet_alani',
        'value' => 200,
        'type' => 'NUMERIC',
        'compare' => '>=',
      ),
      1 => 
      array (
        'key' => '_max_kapali_davet_alani',
        'value' => 300,
        'type' => 'NUMERIC',
        'compare' => '<=',
      ),
    ),
    2 => 
    array (
      0 => 
      array (
        'key' => '_min_kapali_davet_alani',
        'value' => 300,
        'type' => 'NUMERIC',
        'compare' => '>=',
      ),
      1 => 
      array (
        'key' => '_max_kapali_davet_alani',
        'value' => 400,
        'type' => 'NUMERIC',
        'compare' => '<=',
      ),
    ),
  ),
)

This is the "meta_key" key in Wp_Query args. I try to filter capacity in 2 index of this array 0-100 and 300-400 can be selected at the same time. Considering 200-300 is not selected. Currently it doesn't work. Actually it returns 504 error in local which mysql overloads and fails. Am I doing something wrong?

1
  • It’s going to be very very slow. It’s slow enough that it seems to fail completely on your current host. A meta query is not a good approach to this issue. At the very least “featured” and “claimed” seem better suited to a taxonomy. Commented Oct 21, 2021 at 6:23

1 Answer 1

1

Short answer is: Big Meta Queries are incredibly bad and can quickly lead to the MySQL-Server not responding anymore. You should try to rework the meta_query to not have so many clauses.

Long Answer: Wordpress Post Metadata is stored incredibly inefficient when it comes to querying. Best advise would be to never ever EVER store data that you would need to query for into post_meta, but that is mostly not possible.

What happens when you use a big meta query is this: the created SQL LEFT JOINs the post_meta database table for every clause there is to the posts table. That means in your case you have a minimum of 10 LEFT JOINS. The MySQL Server probably is stuck in the calculation phase where it tries to find the best way to JOIN all this data into one view.

What should you do now? As Jacob Peattie said, you should first and foremost rework "featured" and "claimed" into a taxonomy of sorts.

Next Thing you can try is to Speed up the Postmeta Table like written here: (only do this if you know your way around MySQL Databases) http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta

This should take some stress off the MySQL Server.

However, you should really consider changing the type of query you want to make. Maybe you can rework the '_min_kapali_davet_alani' and '_max_kapali_davet_alani' into one single meta key? So that you could use a "BETWEEN" Meta query condition? I'm not quite sure what your site does, so it's a bit hard to find good changes.

If nothing helps, you can try to make direct database queries using $wpdb to get results for the single queries (like "between 100 and 200", "between 200 and 300" etc) and then compare the results and give the posts, that you end up wanting to query directly into the "post__in" parameter.

Happy Coding!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.