0

Records in the Database: 5

The Problem: This code takes long to search 5 records and hangs up after with no success results. If I change the 'relation' => 'OR' to 'relation' => 'AND' it works but that's not what am looking for in this scenario.

Is everything okay in my code?

<?php
$args = array(
    'post_type' => 'shipping-schedules', 
    'posts_per_page' => 5,
    'post_status' => 'publish',
    'paged' => $paged,
    'meta_query' => array(
    'relation' => 'OR',
        array(
            'key' => 'schedules_port',
            'value' => $sfrom,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'schedules_port',
            'value' => $sto,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'schedules_vessel',
            'value' => $svessel,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'schedules_voyage',
            'value' => $svoyage,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'schedules_arrival',
            'value' => $sdate_arrival,
            'compare' => '>=',
            'type' => 'NUMERIC'
        ),
        array(
            'key' => 'schedules_departure',
            'value' => $sdate_departure,
            'compare' => '<=',
            'type' => 'NUMERIC'
        )
    )
);                              
?>
  • Show us what you are doing with those args. Are you using a custom WP_Query, or something else? And a meta query with this many parameters is, I suspect, going to have problems. The relationships between the post record and the meta table is such that these queries are not "simple" – cale_b Apr 29 '14 at 23:25
0

There is nothing obviously wrong with the code, assuming you are pushing those arguments through WP_Query, but the query you are trying to run is not going to be very efficient.

You have 6 meta values in an 'OR' relationship, which is not terribly efficient in itself as all of those rows have to be checked for matches, and all of your meta values are matched on a LIKE, which is a kind of regex, and >= or <= which require all of the rows to be read in order for the comparison to be made.

I'd be surprised if this failed with only 5 records in the table. It might but I'd be surprised, and it won't be efficient in any circumstances. I notice that you have set posts_per_page to 5. That is not the same as the number of records in the table and that is going to reflect the number of $wpdb->post records, not the number of $wpdb->postmeta` records that have to be scanned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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