Search code examples
wordpresspodscms

PODS CMS incorrect query


I have a query like this:

$Record = new Pod('event');
$where_clause = "DATE(enddate) >= CURDATE() AND event_type.name='Training'";
$Record->find('startdate ASC', 100, $where_clause);

It return only 1 item while I have 2 satisfy the query.

I have check with each query:

$Record = new Pod('event');
$where_clause = "DATE(enddate) >= CURDATE()";// AND 
$Record->find('startdate ASC', 100, $where_clause);

And

$Record = new Pod('event');
$where_clause = "event_type.name='Training'";// AND 
$Record->find('startdate ASC', 100, $where_clause);

I got 2 items on both queries.

Can you give some hints to fix this? Thanks in advance.


Solution

  • The code you are using is designed to work with Pods 1.X, but your query itself looks good. With Pods 2.X you would want to do the code below, which combines the pods() global function with its method find and then uses the total method to check the number of items returned.

    $param = array( 
        "where" => "DATE(enddate) >= CURDATE() AND event_type.name='Training'",
        "orbery" => "t.startdate ASC",
        "limit" => "100",
    );
    $pod = pods( 'event', $param );
    $number = $pod->total();
    

    You know have the records in $pod and the total number in $number, which you can use to control the loop when you loop through the results in $pod. If you wanted to you could use total_found to get the number of records that would have been returned had you not limited it.

    Also, have you checked that you really have two records that should be returned. It is possible that when you combine the two queries only one of them meets both conditions.

    You should also check out Perry Bonwell's tutorial where he explains how to query for future events. He uses PHP, not SQL functions to handle the dates, but otherwise he has a similar approach.