1

If I understood well, the WP_Meta_Query from Wordpress allows you to query posts by metas.

But what I would like to do is to fetch metas; not posts. And I would like to fetch those metas without any relation to a post ID.

Let's say that I have those entries in my wp_postmeta table :

meta_id | post_id | meta_key | meta_value
  1269      21       vote_up      1
  1268      37       vote_up      17
  1267      12      vote_down     1
  1266      41       vote_up      3
  1265      98      vote_down     3

The meta_value column here is filled with user ids.

What I would like to do is to have a function that counts the rows

  • by meta_value (user id)
  • by meta_key (vote_up)

And eventually filters the results with certain criteras : ignore lines where posts are not published, for example.

Eg.

function bbpvotes_get_votes_up_by_user_count( $user_id = 0 ){

    if (!$user_id) $user_id = get_current_user_id();

    //meta args
    $metas_args = array(
      'key'     => 'vote_up',
      'value'   => $user_id
    );

    //regular posts args (I would to limit the results to those; so for example unpublished posts will not be returned)
    $post_args = array(
        'post_type' => 'post',
        'post_status'   => 'publish',
    );

    //... 

    return count($returned_metas);
}

Is there a WP core function to do this ? Thanks !

1
  • Finally, a question that isn't about WooCommerce or CSS ;) Sadly, there's not much to say here - the whole metadata API seems intrinsically tied to objects (posts, users etc.). Wouldn't be hard to get a few abstract functions coded that work directly on $wpdb for use in your project - let me know if you need any help and I'll post some suggestions. Commented Sep 21, 2015 at 13:57

1 Answer 1

2

Look at the section on $wpdb->get_results(), it allows you to send distinct queries to the database, which is what it sounds like you are looking to do.

So...

// 1st Method - Declaring $wpdb as global and using it to execute an SQL query statement that returns a PHP object

global $wpdb;
$results = $wpdb->get_results( 'SELECT meta_key, count(meta_key) as votes FROM wp_postmeta WHERE meta_value= ' . $user_id . ' GROUP BY meta_key' );

This should return an array called $results for the user with the following data:

meta_key | votes
-----------------
vote_up  |  15
vote_down|  3

If you want to use a standard function, your other option would be to get all the relevant post_ids, and use get_post_meta( $post_id, $key ) then organize the data in PHP, but the above is more efficient.

1
  • Thanks for the display edits, Pieter. The form was fighting me. Only thing I changed again is to remove the {} which were supposed to make that a code block but apparently didn't.
    – Margaret
    Commented Sep 26, 2015 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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