Use array_count_values() (untested):
<?php
$posts = get_posts( array(
'numberposts' => -1,
'category_name' => 'bird',
'order' => 'ASC',
) );
if ( $posts ) {
foreach( $posts as $post ) {
$species[] = get_post_meta( $post->ID, 'species1', true );
$species[] = get_post_meta( $post->ID, 'species2', true );
}
}
$species = array_filter( $species );
$counts = array_count_values( $species );
$species = array_unique( $species );
sort( $species );
foreach ( $species as $specie ) {
printf( '%s %d ', esc_html( $specie ), $counts[ $specie ] );
}
?>
Related Posts:
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Max length of meta_value
- SELECT max(meta_value) FROM wp_postmeta WHERE meta_key=’price’… stops working when value is over 999
- How can I display all values of a custom field from posts with a certain value of another custom field or from certain post types?
- get_pages sort alphabetically by meta value
- Assign/update the custom field value for all posts
- Order by custom field date with ASC order
- How to break meta values into different items and avoid duplicates?
- WP_query : meta_key with custom rule for specific value
- How to sort category by custom field value
- trim custom field text value and show (…)
- Query post order by post and desc not working
- How to wrap meta values seperated by comma in ? [closed]
- How to create a Custom Meta Box with Name/Value Admin User Input Fields?
- Run a check for multiple meta key values
- Filter by custom field (meta_key) using JSON API
- Removing link ” from ” on meta_value in custom feilds
- Sort alphabetically by custom field
- pre_get_posts : only get posts by wp_usermeta value
- meta_value and meta_key filtering returning no posts [closed]
- IF Custom field value equals ZERO
- List custom field values in alphabetical order without repetition
- How to update a custom field in all posts with the value of another custom field in the same post?
- Update post meta custom field using block editor
- How to add custom filed value after in wp post title
- Sortable Custom Columns not sorting correct
- Sort posts by clicks on download button
- query_posts with meta_value
- Show array of meta_value in Edit Post Coloum
- Making WP_Query limit results by date before today where date is a meta_query
- User query – getting values for custom meta keys/fields
- get posts based on non-single metadata
- Set class if a meta value is set within post archive
- Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title
- Create Shortcode shows only posts with custom_field meta
- 4 Unique Random Posts based on Custom Field Values
- Custom Field: Display only if a specific key is selected outside the loop
- Retrieve IDs from custom field, count and display results differently according to count
- WP Query Args – search by meta_key or title
- Saving multiple custom meta box fields
- get Custom field label (select/dropdown) on front end
- Best Practice for storing aggregate data by date or other criteria?
- Block Editor – Meta values not saved, meta changes to empty array on update
- How can I avoid re-inventing the Custom Fields wheel in my Plugin’s Metabox?
- Using wordpress post data as input for google visualisations
- Does wordpress have something like content-type?
- Order Posts by Closest Numeric Values
- How to get and edit custom fields if in Quick Edit
- Order posts by custom field and if custom field is empty return remaining posts
- Create custom field on post draft or publish?
- Adding a prefix to a post title
- Add a post meta key and value only if it does not exist on the post
- Sorting posts by multiple values, combined
- WP_Meta_Query causing long-running MySQL queries
- query posts and custom post type with meta key
- Duplicate posts being displayed when querying Custom Fields
- Json api does not return all metadata
- Use custom field as a variable
- ACF: How to get the full field name (meta_key) by a field key?
- Tweak Meta for Post to work it for Pages also
- Transferring author information from a custom field to a proper author
- How to select posts from multiple categories and metavalues?
- modify wordpress caption shortcode
- UPDATE on SELECT results. A SQL query to swap Author with Meta Box value
- Linking Custom Fields to Database Records
- Custom WP_Query for WordPress Search Results with meta_query
- Change order of posts
- Meta Query And/Or
- URL Rewrite fires when retrieving a custom value?
- How to store post meta in an array
- Efficiently sort only certain categories by custom field
- Set checkbox as checked by default in a metabox
- ACF form edit front end post title does not change permalink
- How to list Category list in ACF Pro’s Select Field to choose from [closed]
- Can I use a Custom Field as the Featured Image URL?
- Display custom field & value correctly on page
- Copy price categories to custom field
- Sanitize user input fields before wp_insert_post
- Use Advanced Custom Fields – replace line of code that hardcodes the tag into the template
- Advanced Custom Fields and Post Meta Fields Relation
- KEY and VALUE custom field issue
- Stripping and/or altering the content of a custom field (video URL)
- Custom field outside the loop and inside an array
- Display Additional Info
- Add link to wordpress field data
- Adding text box with add_meta_box
- (woocommerce) How to get product id form order id & then retrieve custom meta linked to product?
- Is it possible to manage Multiple Custom Fields with Some Relations
- Retrieving and inserting data in custom fields
- How to add custom fields to images for image source text and URL
- RSS Feed in Custom Field
- List custom field values in alphabetical order without repetition from the current category?
- Incorrect data after modifying post using backlink from wp_die
- How can I duplicate “Add Custom Field” with just JavaScript?
- How can I display only the post titles from a selected category in columns?
- Show or hide custom fields by user’s role [frontend]
- How to display search query as formatted text?
- Problem display
- How to make donation button dynamic on chariti theme [closed]
- ACF in radio button cf7