As others have already noted in the comments, using "posts_per_page" => 5,
is better option than "posts_per_page" => -1,
for your query as it will give you the information you need for your conditional check.
Limiting what is queried is also something to consider – as honk31 mentioned. Along 'fields' => 'ids'
you can also add the following lines to your query.
'no_found_rows' => true
– number of total rows not needed in this
case'update_post_meta_cache' => false
– meta not needed in this
case'update_post_term_cache' => false
– terms not needed in this
case
The $query->have_posts()
with wp_reset_postdata();
is also a bit unnecessary as you can just use $query->found_posts
or ->post_count
directly.
if ( $query->found_posts ) {
echo $query->found_posts >= 5 ? '5 or more pending posts' : 'Less than 5 pending posts';
} else {
echo 'Nothing found!';
}
Related Posts:
- Build a content and excerpt grid loop with paging and options for # of posts
- Why does “get_option” pull in the older value in options.php, rather than the newer value, on submission of a form?
- How to add 2 posts under another post? Formatting should be intact
- Use wp_get_recent_posts with search term
- Remove query string specific key value
- How to prevent WP_Query function from returning all posts when empty?
- Display a specific category of products in shop page and disable code for specific actions
- Get users that likes the post
- Remove echo from shortcode
- Security for data obtained from the database
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- change the default order of posts only for specific categories
- Post not populating for custom post type based on category selection
- Custom query vars filters problem with pagination
- How to get post category list as select in front-end?
- Displaying Event within two given time frames
- Output product category link from WP_Query
- Isn’t Returning Value While Using SELECT COUNT(*) FROM {$wpdb->prefix}
- “pre_get_posts” orderby custom date field in different format?
- How to exclude meta no index pages from wp_list_pages
- WP query posts BUT show the most recent and one per author
- Ajax filter with loadmore button
- posts_per_page displays only 2 posts instead of 4 posts
- Delete post meta by serialized meta value
- Modify post image in full size
- WP_Query To Display Product Of Brand On Taxonomy Page
- Custom Post type Ajax search results
- My query keeps looping infinitely ! how to stop it?
- How can I display a query in a page?
- Display Year and Month from custom field + Age Calculator
- insert thumbnail image from php script
- Weird Behaviour: Not all WordPress Posts appearing
- List sibling pages widget, exclude current page
- How to get category pages to look like a certain archive page?
- How to modify this function to exclude also the post belonging to a specific category?
- a problem in class in class-wp-hook.php
- /page/2/ redirect to 404
- Add adjacent post function inside custom recent post function
- Customize default settings on new sites on multisite
- Check if a menu is empty?
- Dynamically change feature image in customiser
- Setting up the child theme so as to enable right-to-left WordPress?
- Design view breaking on Pages
- Getting a fatal error while updating
- Hooking new functions to actions + passing parameters
- How to break down importing of feeds
- Calculations in functions.php [closed]
- Choose To Display Post Views With An Options Panel
- Prevent Data Resubmission On Page refresh
- Get_avatar filter?
- Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
- How to store post ID’s in cookie or session to display the same posts later
- How to create a php variable using WordPress the_title() function
- register_taxonomy() take much queries
- is there a benefit in using a constant over get_stylesheet_directory_uri?
- WooCommerce Tabs [closed]
- How to check if a PHP string is different than meta field?
- Loop increase number
- Displaying posts based on category
- Countdown to date function?
- calling a custom field value inside functions.php
- Noindex Posts From Certain Authors In WordPress
- Custom post type permalinks do not appear using the link functions
- WordPress check if current taxonomy has child
- This function is deprecated; use mysql_real_escape_string() instead
- get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control
- array_rand not working correctly?
- Enqueueing a code block from an options framework
- Conditional On custom field plugin metabox
- Adding a ‘style=’ bit to image_send_to_editor output
- Post Views Code Hacks
- Closing WP_LINK_PAGES DIV ID w/After Argmuent
- Exclude function from running on a certain page
- Auto create Nextgen Gallery on post creation [closed]
- Hide media for non logged in users
- orderby meta_value_num is not working, giving default order
- How can I modify this code to make the search box include tags and meta
- Replace a single variable with add_filter
- How to preload header logo image in WordPress? like what’s the code and where do I put it?
- Contain multiple page templates in one PHP custom template file in WordPress?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Display metabox galleries on specific page template in admin editor
- meta_query search names when they have middle initials
- display condition based on post term and status
- Custom plugin: how do I call a PHP file if settings option is set to true?
- How to change order inside foreach using wp_enqueue_script?
- How do I get the value of a current user’s custom field?
- Adding extra data to shortcode attributes and pass it to JS with wp_localize_script
- Allow Comments by Default for Multiple Post Types
- Query to show average # of months all accounts with specific role have been active
- How to get User Avatar Image with link to Author and User name in Html Title tag?
- Query doesn’t display text data with apostrophes
- How to get all users by user_login as a json_encode array?
- Postmeta not saving
- Hide the content before the more tag in the “single page”
- how to loop through blog posts in php
- Add attribute to featured image with filter (wp_get_attachment_image_attributes)
- get second ID from array
- Execute multiple PHP Snippets causes error?
- Replace block content with an array