It’s because you’re only checking if ( $loop ) {
.
$loop
isn’t empty, it’s a full WP_Query
object with all the information about the query in it, even if no posts were returned. To check if the query actually returned any posts you need to use $loop->have_posts();
:
$loop = new WP_Query( $args );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
endwhile;
else:
endif;
Related Posts:
- How do test if a post is a custom post type?
- Conditional for single-{post-type}.php
- is_singular() not working if called via callback function of admin-ajax.php
- functions.php conditional tag only for custom post type
- Exclude custom function content from certain pages
- Get template part based on custom taxonomy term
- If on term-page -> get the current term?
- custom post type upcoming post and past post
- changing default comment form arguments [duplicate]
- is_page_template not working as expected
- remove child post from custom post type archive
- if custom posts type exists and there are posts load script
- Need help targeting a custom post type with conditional tags
- Why would this IF statement not work? [closed]
- custom comments on specific post type
- Excluding custom post types
- Register widget only page is a singular of custom post type
- Is there a conditional tag to determine whether the post is _any_ custom post type?
- Conditional Query of Custom Post Type and custom taxonomy
- How to run filter only on custom post type edit screen
- Conditional statement for if archive page has posts which contain certain taxonomies/categories/tags, show those terms
- Custom Post Conditional for Parent & Child Single.php?
- Conditional for a Single Post That Belongs to a Category?
- How to show only the most recent post on my custom post type archive?
- Conditional Tags for specific category of custom posts
- Move Genesis Single Page/Single Post Title
- How to check if post/page or taxonomy post is published by admin
- TinyMCE Buttons on Certain Post Type
- Showing taxonomy terms on custom post type
- Check if custom post is a parent?
- Redirect to URL if x number of days passed
- comments hooks on custom post type
- Conditional Statements
- If custom taxonomy else conditional [closed]
- If tag exists, then echo once
- Show specific content on parent custom post type and all children
- Use conditionals with wp_enqueue_style to attach stylesheet according to post type displayed
- How can I check the rewrite slug of current post type listing page
- How to add custom PHP code in post header?
- $_SESSION Not working after X amount of page views
- conditional based upon the count of current user’s published custom posts
- Custom metabox fields not saving when limited to a certain CPT
- Can not hook into custom post type template with: is_page() conditional?
- Diffrent search templates for different post types
- Conditional Tags If Custom Post Parent & Child?
- Custom Post Query Combined with Conditional Tags
- Conditional statement checking two conditions for CPT and Custom Taxonomy [closed]
- strange conditional tag behaviour [duplicate]
- Custom post type is_singular condtional not working when managing sidebar display
- Custom Post Type, WP_Query and ‘orderby’
- List custom post types in dropdown
- Numeric pagination custom post type
- Categories under custom post types doesn’t show properly
- get a list of posts from Custom Taxonomy
- Custom post type archive sorted and grouped by date in post meta field
- Custom post type and taxonomy permalinks – Structure
- Best action hook to create custom post and assign taxonomy terms to it on plugin activation?
- Add a label to the editor?
- How to get the post type from a category id?
- Custom Post Type won’t Register
- Get image post ID in media upload
- Turn on and off custom post type from admin?
- How to make this menu drop down or like accordion
- Several post types on WP Query by tag and taxonomy
- Custom Postype specific changes in admin panel
- Can’t publish custom post type – “You are not allowed to edit this post.”
- Display and register custom categories for custom post types
- Warning: Invalid argument supplied for foreach() in post.php [closed]
- Adding predefined terms to a taxonomy
- Custom post type – list posts based on meta_key and display them divided by CPT taxonomy category
- Get $post->ID from a URL
- Redirect to archive is single post has a certain term assigned to it?
- Saving fields in a drop-down in WordPress
- Custom taxonomies to define versions of a product
- why is the current page title being output?
- URL Rewrite – Page Archive
- How to prevent meta data from being edited in a post?
- Getting WordPress to store 0 values for custom post type meta
- WP Query from two Custom Post type fields as statement
- Save data is post using php front end
- Custom Post Type Archive Page showing as 404
- Custom page with no headers, footers and its own CSS, Javascript
- Custom Post Type Single Page and Archive Page redirects to Home Page
- Videos Post Type with Custom Filters
- Pull 2 different custom post type with 2 different WordPress into external website
- Multiple Custom Post Type in Taxonomy Archive Causing White Screen
- Adding a Section for Visitors
- Use output of wp_dropdown_categories($args)
- How to Create Single template for custom post
- If thumbnail image else post title
- How save custom meta type posts multicheck grouped by taxonomy (with CMB2) [closed]
- Add filter button to custom post type in admin area
- Associate custom categories to different user IDs
- Cant move file after media_handle_upload
- archive-{custom_post_type}.php not getting recognized wordpress
- Comments not enabled for custom post types
- How to rewrite CPT-Permalinks the correct way (incorporating meta-box-data)?
- Checking for published posts in a certain post type
- Allowing logged in users to create custom posts
- Custom Post Type and single-posttype template