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
- Admin notice not displaying
- Can’t get order_by meta_value_num to work properly
- Create custom post type on successful woocommerce order [closed]
- Adding custom tables to WordPress
- right_now_content_table_end function not working?
- WordPress count child posts of each parent custom post type and use in json array for google maps jquery plugin
- Taxonomy Archive: Display only one post per term from separate custom taxonomy
- Very Slow Page – How to Optimize # of Queries?
- Dynamic page outside WordPress
- Using wordpress template tags within an array
- How to display Custom Post Type Post based on Tag with Shortcode Parameter?
- Getting the URL of the parent page
- Custom query to filter posts that have current post as a taxonomy [closed]
- Custom post type permalink returns bad url
- Return the thumbnail meta data for getter and setter
- How to display selected taxonomies by their parent
- Custom Field Order by Last Name and First Name
- Pagination for custom php code
- get_posts of Custom Post Type AND Custom Taxonomy
- What’s the difference between same wp functions get_posts(); functions in different form?
- Changing number of posts per page on CPT-archive.php, have tried 20-30 code chunks so far
- Custom Post Type Loop throws 500 error when used in widget
- how to check if custom post type column already exists?
- Get posts from a custom post type by child categories of a parent category
- Query/list all terms and their custom post count
- Received nothing after executing AJAX post function
- Custom Post-Type not in admin menu
- get the custom post type title/name
- Page vs Custom Post Types Differences/Issues
- If post has custom field then display css-class
- Push metadata in array
- How to customize a permalink (URL) structure?
- Troubles with saving metabox
- CPT + CMB2: data not displaying for only first post in loop
- How to make a field appear only if a post meta field has a defined value?
- Replace text in post from cvs
- Get parent categories of custom post type
- Can I list a custom post type within another custom post type in the admin area?
- Get custom post type list for every category shortcode
- How do i calculate the total of values of custom fields in custom post types?
- I would like to have different styles for my posts based on the content of each post
- Checking if Post Title is Unique as Loop Criteria
- Query & Order posts by custom fields
- Custom post type – get_day_link()
- Set URL link to featured image of custom post type
- Custom Tag Description unable to display just below and outside of the Loop
- AJAX load more for different custom post type loops
- WP_Query to select custom post type with Advanced Custom Fields (ACF) date
- On update or create post redirect to current post position in list
- I need to add endpoint for wordpress categories
- I’d like to cycle through existing posts in a post type, showing one per day ordered by title, and starting over when the last is reached
- 404 error when navigating to a taxonomy page with Cyrillic, what’s wrong?
- Changing wordpress publication date to ACF date and time picker date not working
- Why are taxonomy terms not saving when using custom meta boxes with radio button taxonomy selectors?
- Use custom metabox to update automatically a post after a given date
- get_post_meta not working on publishing
- creat filter with wp_query
- Post count by month of taxonmy term
- Redirect 404 page with ID in slug to associated page with same ID in slug
- WordPress wrapped added a span tag to every single p tag
- How do I add a custom screen for a custom post type to be called from custom post row action?
- ACF field key/value to show on taxonomy list
- WordPress custom post type permalink rewrite shows page not found
- Using custom field content as expression in IF statement [closed]
- Can one create multiple Custom Post Types with a for loop?
- How to Upload CSV Data into Custom Post Type Data with Metabox programmatically
- How to display custom WP menus?
- Categorize custom post type
- How to Create Columns with WordPress and CPTUI display page ?
- custom post type category count shortcode
- Custom post type editor with dynamic selects, one drop down populating a second second drop down not working
- WordPress custom post type split into pages
- How to add custom field to view-edit post type page
- Assign External Database Queries to Global Variables and Make Them Accessible
- Hide a widget inside a div on specific type of post
- How to import a custom MySQL DB table into a WordPress custom post type
- Static apple-app-site-association file with wordpress
- search suggest – filter post type
- Breaking up CPT output into two columns
- Function causing the loop display posting more (empty)
- Make a custom_post translatable
- Display latest x posts from all categories in Custom Post Type/Taxonomy
- How to update post meta on uploaded image from a custom form?
- Foreach inside shortcode not working as planned
- Configuring a meta query with multiple post types that have the same relationship on a single page
- Displaying custom field according to date
- How can I move (or create another) publish button?
- Different Category system needed for the Custom Post Type
- Problems wp_insert_post and save_posts filter
- Specifying Mouseover Icon for Custom Post
- How to display author post count for multiple custom post types?
- Add additional field to custom post_type
- Meta-Boxes for CustomPostType cause PHP Errors and Notices in “Add New” view
- Category.php template for custom posts
- categories should be available across all custom post types
- Set featured image to archive.php
- how to echo meta tag in header using php
- Get term name without a foreach loop
- Cant get paginations on single.php to work with my custom post types