• Resolved Jo Cox

    (@jocoxdesign)


    I’m trying to add the values of all post meta entries for a specific author and getting completely stuck. So far I have this, which adds every single entry (half the battle). However I only want the current user:

    $values = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'spt_cpdlog_points'"); $cpdlogpoints = "" . array_sum($values) . "";

    I’ve tried using a LEFT JOIN to cross reference the posts table with the postmeta table but I can’t get it to work. Anyone able to help?

Viewing 1 replies (of 1 total)
  • Thread Starter Jo Cox

    (@jocoxdesign)

    Sorted it:

    $values = $wpdb->get_col( “SELECT meta_value
    FROM $wpdb->postmeta AS wppm
    JOIN $wpdb->posts AS wpp
    ON wppm.post_id = wpp.ID
    WHERE wpp.post_author = $post_author
    AND wppm.meta_key = ‘spt_cpdlog_points'”);
    $cpdlogpoints = “” . array_sum($values) . “”;

    For anyone who sees this in the future!

Viewing 1 replies (of 1 total)
  • The topic ‘MySQL query to add postmeta values for a specific author’ is closed to new replies.