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
- Add a Jquery Datepicker to custom field in post edit
- How can I avoid re-inventing the Custom Fields wheel in my Plugin’s Metabox?
- Does wordpress have something like content-type?
- Order Posts by Closest Numeric Values
- How to get and edit custom fields if in Quick Edit
- Create additional short URL with custom field and 301 redirect
- Create custom field on post draft or publish?
- get_post_meta fields don’t show up on posts page
- Using custom fields in a filter hook
- Function to change meta value in database for each post
- Include woocommerce custom field value in front-end search result
- Ordering Posts Type A by Custom Fields of related Post Type B
- 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
- How to validate custom field on lost password form before the user id field?
- MySQL Query that looks for post with Custom Field, then changes Category
- Woocommerce: Custom fields and email shortcodes, ec_custom_field
- Tweak Meta for Post to work it for Pages also
- Create Pop-Up Box with Custom Field Content Inside the Loop
- Adding an editable field to template?
- Transferring author information from a custom field to a proper author
- How to select posts from multiple categories and metavalues?
- jQuery – Automatic event trigger does not **really** check my form
- Display URL in a Custom Field
- Linking Custom Fields to Database Records
- Stop sending email everytime I updates my post
- Best way to store everyday post views?
- Display list of terms having the same custom field value
- How to display custom field in woocommerce email orders? [closed]
- Custom meta is not being saved
- How can I activate custom fields checkbox in pages?
- Can I save post meta programatically without setting metaboxes?
- How to add a css class to custom field value?
- Show custom field on attachment page?
- Advanced Custom fields not available yet during publish_page action
- Meta box values are displayed on Custom Fields list. Is it possible to hide them?
- Sort posts by custom field value
- WP Insert post with post_thumbnail
- How to pass custom field value to shortcode?
- custom comment_form fields not displaying
- WP_Query () using taxonomy, custom post types
- use custom field value as post category in loop
- Searching with Multiple (Parallel) Criteria on Custom Fields?
- Save custom field types with this function…?
- Compare ACF date field (if exists) and post date and order DESC
- Add WordPress user custom meta to chartjs vertically stacked chart in Divi theme
- Query posts by post type, author and post meta
- Delete a custom field in mysql for all posts with specific category id
- Trying to get custom field data in a simple plugin
- Sort columns in the ‘Pages’ dashboard overview
- Create Meta boxes dynamically
- Outputting custom field on home.php not outside of blog list
- I have a form with a custom button ,i want to add the field data to an database when clicking the custom button
- Add capability for editors to modify custom fields
- Custom Field select list is truncated
- Meta_query not filtering posts
- Rewrite Custom Fields url
- Display custom field outside the loop