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 !
$wpdb
for use in your project - let me know if you need any help and I'll post some suggestions.