[resolved] What is an efficient way to query based on post_meta? (2 posts)

  1. tyler1987
    Posted 2 years ago #

    The problem I am having is when I allow users to filter posts it overpowers the database and things run extremely slow. I am unsure what the best way to query posts using meta values is. My site has a form that users can submit to filter posts based on checkboxes containing meta values. I currently have the site making an array out of the checkboxes selected for individual post_meta keys like so:

    for ($i=0; $i< count($amenities); $i++)
            $count = count($arrays);
            $arrays[$count] = array(
                        'key' => 'amenities',
                        'value' => $amenities[$i],
                        'compare' => 'LIKE'

    Then I query the database using an array of arrays which contain the values selected for each meta_key. Like this:

    $the_query = new WP_Query(array(
            'post_type' => 'listing',
            'post_status' => 'publish',
            'category_name' => 'private_rental',
            'orderby' => 'rand',
            'meta_query' => $arrays

    When I do this I get the results I want but it can take over a minute for the results to appear on the screen. Is there a more efficient way of doing this?

    Any help would be appreciated!

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    There's nothing you can do in the the code to make the query any more efficient. What I suspect is happening is that you have a very large database and it's simply taking a long time to run the MySQL query. Or perhaps the MySQL server is slow. You could look at moving to a dedicated server to see if that helps.

Topic Closed

This topic has been closed to new replies.

About this Topic