Saturday, January 8, 2022

[FIXED] Counting post meta based on key and value and get meta count, not post count

Issue

I'm using WordPress meta data to register clicks on images, to know which images each user has clicked - and also the total number of clicked images per user. The first part is fine, but I'm struggling to get the counter going, as it's returning a lower amount of meta data than what is actually there.

I have a custom post type gallerier and each gallery has a number of images. I'm using the meta key nedlasting, and I'm identifying each image individually by fetching the url.

Here is how I register clicks, after checking it isn't already:

// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {

    $brukerid = (string)get_current_user_id();

    // Check if the image is downloaded previously
    $args = array(
       'post_type' => 'gallerier',
       'meta_query' => array(
           array(
               'key' => 'nedlasting',
               'value' => sprintf(':"%s";', $url),
               'compare' => 'LIKE'
           ),
           array(
               'key' => 'nedlasting',
               'value' => sprintf(':"%s";', $brukerid),
               'compare' => 'LIKE'
           )
       ),
       'fields' => 'ids'
    );
    // Perform the query
    $nedl_query = new WP_Query( $args );

    $nedl_ids = $nedl_query->posts;

    // If not already downloaded, register it
    if ( empty( $nedl_ids ) ) {

    $metaarray = Array(
        'user_id' => $brukerid,
        'url' => $url,
        'date' => $dato
    );

        add_post_meta( $postid, 'nedlasting', $metaarray );
    }
}

Then I'm trying to count those registered clicks using the following function:

// Count number of downloads for a single user
function tell_nedlastinger() {

    $brukerid = (string)get_current_user_id();

    $args = array(
       'post_type' => 'gallerier',
       'meta_query' => array(
           array(
               'key' => 'nedlasting',
               'value' => sprintf(':"%s";', $brukerid),
               'compare' => 'LIKE'
           )
       ),
       'fields' => 'ids'
    );
    // perform the query
    $nedl_query = new WP_Query( $args );

    $nedl_ids = $nedl_query->posts;

    return count($nedl_ids);
}

The function returns a number, but always much lower than the actual amount of registered meta data/clicks. Anyone seeing a problem?

Edit: I'm pretty sure the problem is that I'm getting the total number of posts, not the total number of meta data entries/clicks - which more often that not is several per post. Any way around that?


Solution

I never found a solution to query meta data and get x results per post, so instead of using an array in a single key I split the post meta data into three keys. Then I used $wpdb for a custom query. The final code for the tell_nedlastinger() function:

$brukerid = (string)get_current_user_id();

global $wpdb;
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");

return count($query);


Answered By - danjah

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.