WP_Query
provides some useful properties. There are two of them which you could use:
$post_count
– The number of posts being displayed (if you not passposts_per_page
argument to WP_Query construct it will return at most 5 posts)$found_posts
– The total number of posts found matching the current query parameters (so if you have got 100 posts in database which will fit to the arguments then this property will return 100)
Here is sample of code:
$args = array( 'post_type' => 'testimonial' );
$loop = new WP_Query( $args );
$numposts = $loop->post_count;
if ($numposts == 1) {
// do X
} else if ($numposts > 1) {
// do Y
} else {
// do Z
}
Related Posts:
- How to make sure content doesn’t display if selection is empty
- How to display categories of my Custom Post Type?
- Count posts or custom post types from last 24 hours (or from today)
- How do I programmatically add items of content to a custom post type?
- How to get a list of term names of the custom post type im currently on in single.php
- is_singular won’t call my functions?
- taxonomy list display custom post count
- Ajax filter with custom taxonomies
- Custom Post Type Category List & Post Count
- Custom post type loops with different page templates
- How can I programmatically save data into custom fields that contain serialized data?
- Row actions not showing? Why?
- Displaying custom post type on front page
- Custom Post Type .current-menu-item not applying on Custom Post Type Archive Page
- How do you output custom code between posts in the loop?
- How to allow “Add New” capability of CPT when links to its UI are placed as a submenu?
- Dynamic Custom Post Type Plugin
- How to filter out post type meta?
- How to make in WordPress admin panel sortable column for the custom field, that count the number of page impressions?
- Output custom post shortcode. Help spot the error.
- Blog page showing same content as homepage
- Custom Post Type Taxonomies -Posts not showing in Category or Tag pages
- A good strategy to print custom posts (offer) that are checked inside the metabox of a post?
- Can’t get order_by meta_value_num to work properly
- Create custom post type on successful woocommerce order [closed]
- Very Slow Page – How to Optimize # of Queries?
- Custom post type permalink returns bad url
- How to display selected taxonomies by their parent
- Custom Field Order by Last Name and First Name
- get the custom post type title/name
- Push metadata in array
- Get custom post type list for every category shortcode
- How do i calculate the total of values of custom fields in custom post types?
- Custom post type – get_day_link()
- Use custom metabox to update automatically a post after a given date
- get_post_meta not working on publishing
- WordPress wrapped added a span tag to every single p tag
- How to Create Columns with WordPress and CPTUI display page ?
- WordPress custom post type split into pages
- Assign External Database Queries to Global Variables and Make Them Accessible
- How can I move (or create another) publish button?
- Different Category system needed for the Custom Post Type
- Specifying Mouseover Icon for Custom Post
- Assign category a default post type
- Check IF single OR multiple custom data has been entered
- I am trying to get cutom post cout by month of current taxonmy term
- Get data from PHP to JavaScript to set position of each post on front page
- Removing full title and breadcrumb header container for a specific post type
- Query custom post types & Taxonomies and list them in a table on a page
- Creating a “glossary” of WordPress posts of a specific post type
- Using Pagination for Custom Post Type and keeps returning 404
- Nested Custom Taxonomies | Incorrect posts when querying
- I’m trying to create a custom version of the wp_list_authors function that includes custom post types
- Validate Custom Post Type fields
- meta query not retrieving posts
- How do I get the index for a custom post?
- Commas not displaying in implode
- add custom tags for custom post type in wordpress
- How do I show my containing my custom field ONLY if there is a set value on that custom field? [closed]
- CPT Repeatable Fields + Undefined Index
- foreach loop inside the loop creating duplicates in output
- Custom Post Type Causes Error: Warning: call_user_func_array()
- Is it a connection or relationship between 2 custom post types?
- redirect after submiting post for review
- CPT loop doesn’t seem to account for post date?
- Custom front-end form for adding post – Category problem
- Get only parent post in archive post
- How to change Elementor Template for Single Page Post on Load
- update custom taxonomy custom fields
- Filter taxonomy by CPT
- Alike Shortcode using in Custom Shortcode
- display post count in archive page that have relation with another taxonomy term
- Sort custom post column by generated value?
- Why it doesn’t create a custom post type after form submitting
- Custom post type order by post_title
- Tell wordpress to show a single page instead of an archive page
- Get meta values from parent post and save in child post
- Using WordPress custom post type to display StoryMapJS
- Change Search display for Custom Post Type
- Stored meta from attachment, video length?
- tax_query not working?
- PHP Notice: Unidentified index
- Duplicate posts in my custom loop
- Include images from pages in wp search.php results in default wp search
- WordPress loop add heading before first of type
- How to make a shortcode for my WP_Query Loop? [duplicate]
- get_post_meta for Custom Post Type ( CPT )
- How to display all custom fields associated with a post type – IN THE ADMIN AREA?
- Meta box not saving
- Featured image in custom post is being disabled
- Can’t pick up a field created with Advanced Custom Fields
- Pods pagination erases my sub menu
- Custom Search not working
- How to loose the image ‘alt’ attribute in my custom post type?
- Custom Post-type not returning the right child_of
- WordPress Set A Static Page/Template For All Sub-Pages
- Add HTML before a specific div?
- How to hide home title on pages and posts?
- Is there any way of not using my_init_method in the following code (that creates a custom post type)?
- Display ONLY ONE $term (Out of 4 terms) from a Custom Taxonomy and CPT