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.
Related Posts:
- How to display liked posts of current user in wordpress?
- Order by meta value pro first then meta value free in my search function
- get_post_meta with WP_query
- Is there any way to get all custom posts and all custom terms with it’s meta in few queries?
- What is the best way to query posts based on live data?
- How can I create a meta_query with an array as meta_field?
- How can I query all users who registered today?
- Search custom post type by meta data
- Custom Queries: Joining On Meta Values From Two Custom Post Types
- WordPress creating excessive joins on meta_query with search
- Filter query posts by the count of metas for a meta key
- Save default value for empty or missing post meta
- Grouping related postmeta data via SQL query
- How can I apply a meta query for a single custom post type in the main query?
- How do I find if a page has a template?
- mySQL query. ORDER BY meta_key
- Remove posts from query for events whose start date has passed
- How to check current user in meta value array in WP_Query meta_query
- How to orderby multiple meta fields with another meta query
- Issue with using add_rewrite_rule() for post querying custom fields
- Query returning same results even though the ID changes
- Query posts according to specific post meta values
- get_users with array as meta_value
- Declaring a var, placing it in a query and using the output of the query?
- WordPress Mysql query and Duplicate
- WP_Query meta compare must include ALL array values
- Combining two meta_values within one row with query
- Excluding posts by meta, and also keeping posts without the meta
- How to “orderby” the first array in a meta_query that uses multiply keys?
- meta_query not working
- WordPress query: merge meta key (number) values and sort
- Query against multiple locations within single custom post type post
- Restrict WordPress search to a single ACF field
- Sort by postmeta on when searching
- Fastest and most efficient SQL query to check if UID exists
- Use $wpdb->get_results with filter based on array
- Query Posts that have or don’t have a meta_value and order by the same ASC
- meta query always fails even if data is correct
- Custom MySQL Query for Post and Post Meta
- list or get meta_key where meta_value is ‘something’
- WP_Query orderby meta key/value suddely stopped working
- Meta Query compare with LIKE pulls similar post types: 55 and 155, and 1,155
- Get Terms by IDs with IDs order
- Query WooCommerce orders where meta data does not exist
- Meta compare with date (stored as string) not working
- Slow wp_enqueue_media()
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- Sorting Grids with Essential Grid and Events Manger
- The use of including upgrade.php when building custom queries
- Get the timout value of a saved transient?
- Does meta_query work within get_posts array?
- WordPress Find Duplicate Post By Content
- WP Query Sort by meta value (date)
- how to query multiple categories in wordpress?
- Disable the MySQL query in the main query
- Update slug (URL) of pending posts via phpMyAdmin
- How can I get the query that would be run for the archive page?
- Missing posts in a query
- Alter a specific query on WordPress
- Meta query for custom post type ignored in main query
- SQL query to select posts from multiple categories
- Protect sequence of pages with same password for each of two groups of users
- WP_Query with meta_query for children
- How is it possible to get top comment from all children?
- how to make members list directory through wordpress post custom meta key.
- Select Query demand excesive resources [closed]
- Create Indexes for Slow Queries
- Compare meta_query with a Regular Expression and do a less-than operation on it
- I want to create a filter for the query string is this possible?
- Exclude categories from the_category();
- How can i show all categories using wp query?
- All in One Calendar Plugin Custom Post Type Query [closed]
- Get Posts from Last 24 hours and Sort them via GD Star Rating
- Why my query ‘REPLACE INTO…’ does not work?
- get_post_meta as a list for drop down search filter
- Restrict query to last day with posts
- Custom query object with Simple-Fields custom date field
- How to set up hierarchical relationships without using plugins / meta query
- WP_Query of custom post type sorted by meta_key has unexpected results
- Customizing the_tags output?
- Querying with WP query using meta key price
- Re-sort get_posts query results
- Taxonomy filter under Polylang
- create a link to a random post within the current category
- meta_query where the meta value is not the post title
- Filter posts by meta key
- Pagination not working on custom post types with rewrite slug
- Change pure SQL database query to WordPress post query?
- Unable to gather Image URL from Custom Post Type’s; Custom Meta Field
- Pulling an ACF into a query
- How can I modify standard search query to include also ACF custom fields values?
- WordPress search form and search result through ACF field in custom taxonomy
- Using cron for multiple queries
- Return a single custom post from multiple meta queries
- Custom Query problem access the values
- Set posts per page for parent category and it’s all children
- Exclude sticky posts from query
- Rewrite Query_Var URL Parameter with Slug
- How to query different categories on index?
- ‘Active lotteries’ only custom query for woocommerce lottery plugin and elementor