If you use those custom fields only for the company tagged posts, then it’s sufficient to retrieve the custom fields without worrying about posts and tags:
$tags = array('Professional Rating', 'Efficiency Rating', 'Referral Rating');
foreach ($tags as $tag)
{
$result = $wpdb->get_col( "SELECT meta_value FROM wp_postmeta WHERE meta_key = '$tag'" );
$avg = empty($result) ? 0 : ( array_sum($result) / count($result) );
echo "Average for $tag: $avg" . PHP_EOL;
}
This doesn’t check if the company posts are published or not.
For that use this query:
SELECT meta_value FROM wp_posts p, wp_postmeta pm
WHERE p.ID=pm.post_id AND p.post_status="publish" AND meta_key = '$tag'
Related Posts:
- Get posts with tag all post types
- Search Custom Post Type with all meta attached?
- Query for custom posts with a specific tag slug
- How to use custom post type APIs, but use a different db table
- Paginated Taxonomy Term Archive including one post per term
- Where to store custom tagging data associated with a custom post type
- wpdb custom post_type problem
- Why does querying on post_tags (which has been applied to custom post types) only return posts?
- Can’t query tag and post_type at the same time
- WordPress loop: Show only a Custom Post Type Taxononmy TERM
- How to get all tags of a custom post type by id
- WP_Query orderby and tax_query
- can these 3 queries be re-written as 1 query?
- How to list tags from custom post type attachments?
- Display related CPT with custom taxonomy
- Custom post type query with taxonomy
- how to display posts content on the custom css popup by clicking on each title on the sidebar?
- How select query is generated in a Custom Post Type?
- CPT or Custom Table for Repository of Serial Numbers
- tax_query showing no results
- Storing/querying custom date data
- Query Custom Post Type by Tag
- Attaching multiple custom taxonomies to one CPT?
- Search multiple custom fields by using meta_query
- post_tag taxonomy with custom post type
- category__in not working on custom post type
- Displaying Posts Related to Other Posts by a Taxonomy Term?
- How to query for posts (in hierarchical custom post type) that have children?
- Set default option in dropdown of WP_Query?
- WP_Query pagination using only numbers instead of /page/1 on URL
- Query for posts in 2 taxonomies
- Categories and Tags not working!
- How do I create an archive page for standard posts?
- Multiple post type queries (with specific arguments for each)
- Custom WP_Query not returning results when querying custom post type
- Can custom taxonomies items have attached properties?
- Custom Post Type Loop within Shortcode
- $wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies
- Combining sorted and random CPT
- How to add post_type=value when editing that post type in the WordPress admin?
- Display CPT posts based on specific taxonomy
- Filtering WP_Query Dynamically on the Front-End
- Why get_posts() not returning only selected category posts from Custom Post Type?
- How to use custom taxonomies to reference complex relationships?
- Is it possible to store Custom Post Type data in separate set of tables and still have wp_post class functionality?
- Prioritize posts in query by meta keys?
- Cross reference custom post types
- Possible to filter custom post type with multiple meta data?
- Show all tags on custom post type
- Specifying meta field’s column type in Database using add_post_meta
- Loop increase number
- get_the_tag_list() returns bad links
- Custom filter for main search: how to exclude specific post_type from search results
- Is This Code Efficient – Or is there a better way?
- Inefficient Query Confusion
- find custom post type post by searching its custom field with my string
- wordpress lists similar type of posts in a custom post type
- FacetWP paging custom wp_query
- How to get_term_meta on single custom post?
- Cannot get custom posts by category
- Shortcode with WP_Query more than once on one page
- Can’t change posts per page in WordPress Post Type Query
- How to constrain the results to a specific post_type on tag archive page?
- Creating a Tabbed Widget
- How to Display the tag content of Custom Post?
- list posts of two post types in a single template
- Using WP meta query to show custom post types by a start and finish date
- How do I list a custom field and custom taxonomies for each result in a loop?
- Find Posts based on Child Post value
- ACF – Retrieve custom taxonomy from a relationship field
- Sort custom posts by date and then by taxonomy
- Query custom post type that has a serialized relational advanced custom field value
- Right way to store a large set of similar information on website? [duplicate]
- build child and anchestor three from post parent
- How can I query and sort custom-post type using WP_Query
- Custom query result empty on page 2
- WordPress doesn’t respect Page slug in custom post type url pagination. Keeps getting removed
- Create 3 Levels of relations with WordPress
- Output all terms slugs for a loop filter
- WP_query sort by taxonomy
- Linking posts together with Advanced Custom Fields “both ways”
- Multiple WordPress Sites, Same Database but Filtered Content
- Error get_posts with Custom Taxonomy and OR relation
- Custom Field as Custom Post type element class
- Display posts on parent post if author coincides
- How to get Current week and current date record wp query
- Error in WP Query. If variable is empty it is displaying previous post value
- Do not output category and tag URLs
- WP_Query function for custom post type
- How to pass taxonomy terms to WP_Query along with $args?
- How can I base a query off of the current page post type AND a meta value from another custom post type
- Query Posts From Multiple Post Types
- Fallback if statement based on the number filtered from it
- Insert data in custom table during new post creation
- Most efficient way of deleting post
- Problem with menu categories doubling up when updating database?
- Switched server & domain name, now custom post types won’t save
- Get images only from a certain post type
- Can WordPress show posts based on a button that the user clicked 2 pages back?
- querying to custom field over ACF REST API