Compare two numeric custom fields

I use Advanced Custom Fields and have a custompost-type “matches”. This post-type contains two fields “goals-made” and “goals-against”.

I want to query wordpress to show only the matches that were won. So where “goals made” > “goals-against”.

Can anyone help me get started on this one. The goal is to have a statistics page in the end.

Thx a lot for helping me out

4 Answers
4

I think something like this, but not tested, and my SQL-foo is rather weak:

    $test = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT DISTINCT    $wpdb->posts.*
    FROM               $wpdb->posts
    INNER JOIN         $wpdb->postmeta AS mt1 ON (
         wp_posts.ID = $wpdb->postmeta.post_id
    )
    WHERE              $wpdb->postmeta.meta_key = 'goals-made'
    AND(               mt1.meta_key = 'goals-against'
        AND            CAST($wpdb->postmeta.meta_value AS INT) > CAST(mt1.meta_value AS INT)
    )

    "
));

This should return a list of post_ids that you can then process. But then again, it might just fail miserably.

Leave a Comment