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?
- Pagination : How to remove /page/x/ after a ‘POST’ action on a form returning to page 1
- Can I create a loop with multiple post types and specify different $args for each post type?
- How to display Custom Post Type Post based on Tag with Shortcode Parameter?
- How to show CPTs in term archive
- Query custom post type by category slug
- Combining sorted and random CPT
- WP All Import – Using Xpath to set a select value
- How to add post_type=value when editing that post type in the WordPress admin?
- Display CPT posts based on specific taxonomy
- Group posts by custom post type
- Setting proper query for multiple custom admin filters
- shortcode to show custom post types by category
- Tag page with Custom Post Types not returning any posts
- Unable to retrieve any posts of CPT in wp-admin
- WP_Query: include custom post type only with specific meta value
- Pagination on a custom post type loop
- showing custom post types of a certain category only
- WP_Query orderby modified to include custom meta changes
- WordPress custom loop filter by meta_key and value with serialize data
- Returning a list of custom post types excluding those without a specific meta_value
- query private custom post type
- Display custom post type from dynamic custom field
- Query/list all terms and their custom post count
- Linking to the most recent post in a Custom Post Type
- How to make sure content doesn’t display if selection is empty
- Filtering WP_Query Dynamically on the Front-End
- Show one item per category of a custom post type
- Sorting custom post type columns with external data and without meta values
- Why get_posts() not returning only selected category posts from Custom Post Type?
- Storing posts from query and accessing later via AJAX call
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Genesis filterable portfolio isotope [closed]
- Get posts by querying taxonomy and certain terms of the taxonomy?
- Query custom post type with tags
- wp_query check if integer exists in custom field’s array
- Custom post types and tag archive pages/permalink structure issue
- Tag Archive for Custom-Post-Type Posts yielding 404 when permalinks set to postname
- Custom post type templating problem
- convert custom query to wp_query
- Wrong request query on cpt and tax
- Previous/Next Link by Meta Value in CPT
- Show custom post type filtered by category
- Randomize Posts. Skip the first post in ascending order
- Creating a navigation menu of all posts of a custom post type and their children posts?
- Get custom taxonomies from multiple posts
- Pagination for Custom Taxonomy Page [duplicate]
- Filtering WP_Query
- Search / Filter posts on Title/Content OR Tags
- Pagination on with query_posts in custom post type template
- Custom Post Type Taxonomy Filters
- Wp_query: sort by PHP variable
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Custom-Posttype & Custom Taxonomy WP_Query
- Crafting WP_Query array, sort by date
- Custom loop with multiple taxonomy queries
- How to use custom taxonomies to reference complex relationships?
- Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
- Converting the_content string to an array?
- Create a clone from one WordPress site to another in a few minutes
- search also in taxonomy, tags and custom fields
- forming WP_Query for posts of all post types but from specific categories
- Get latest 3 posts from multiple CPT in one query
- Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
- Custom post type getting wrong categories and tags
- Display upcoming Events for next 7 Days
- Listing tags from a custom post type
- Isotope Filtering with Bootstrap Tabs – Custom Post Type Query Loop in each Tab (Have to click twice to filter)
- wp_update_term_count_now doesn’t work with a custom post type?
- Add tag to a custom post type doesn’t change the total of custom post type in backend
- Custom post ui plugin & WP_Query – Polylang
- Custom Tag Description unable to display just below and outside of the Loop
- How can I get the number of custom post type posts that have a specific attachment image set?
- How to use load more custom post type data normal or according to on click category tab In WP
- orderby rand is not working for a custom post types
- Problem querying Custom post type by custom fields
- Orderby CPT custom fields not working