You can check if a query has posts with if( $your_query->have_posts() )
, and only output the container if it passes that test.
$featuredPosts = new WP_Query( array(
'posts_per_page' => 2,
'cat' => 6,
'offset' => 4
) );
if ( $featuredPosts->have_posts() ) :
echo('<ul class="right-hang">');
while ( $featuredPosts->have_posts() ) :
$featuredPosts->the_post();
endwhile;
echo '</ul>';
endif;
Related Posts:
- Can I force WP_Query to return no results?
- Use the page slug in a WP_Query?
- paginate_links() adds empty href to first page and previous link
- Using OR conditions in meta_query for query_posts argument
- Get array of posts from the current archive page loop
- Get posts by menu ID
- count posts from custom taxonomy terms by year
- How to grab metabox value in wp_query meta_query key
- Woocommerce query by price range and custom meta key
- Get posts with condition on comment meta value
- Add and in the header while looping over custom query in page template
- Stop all SQL_CALC_FOUND_ROWS wordpress queries
- Move posts to top of WP_Query if in certain Taxonomy?
- WP_query ‘orderby=none’ Problem
- Pagination not working on static page
- How can I create a WP_Query that returns posts where one meta_value
- ‘paged’ in wp_query doesn’t work
- Sorting meta_value as integer doesn’t work
- Get posts from the Main site to sub site – WordPress MultiSites Network
- Custom Field Query – Meta Value is Array
- Get array of current post term ID’s
- WP_Query for showing specific post by id
- Ordering by meta_value AND date NOT WORKING with wp_query
- Query by custom dates in UNIX Time
- Custom WP Query from meta_value stored as serialised array
- Implementing an OR statement to wordpress wp_query
- Get List of all the Authors
- How to check if a post “does not have term” in conditional statement?
- Minimize database queries to user tables?
- WP_Query returns more results than expected
- How to make Meta Query case sensitive?
- Populate tax_query terms parameter with post term
- Orderby the sum of several numeric custom meta fields
- unable to retrive parent page information
- Site not getting correct featured image from my query
- Using WP_Query within an mu-plugin
- wp_query ‘s’, search filter with pagination is not working
- Use more than one query, but prevent duplicates – at scale!
- WP Query related posts by tags
- How to add posts, manually, to a wp_query?
- wp_reset_postdata() does not work as expected
- Search Query for multiple categories using ‘OR’ but having certain categories be ‘AND’
- WP_query with OR relation between args[‘s’] and arg[‘meta_query’]
- How to display an other custom post type in a different custom post type’s archive?
- Replacing raw database query to WP_Query
- Stale $GLOBALS[‘post’] after custom WP_Query empty result set bothers my template, whose fault is this?
- WP CLI can’t delete plugin related table
- Inserting HTML to close and open divs in WP_Query loops
- How to count posts with specific arguments
- Query only the posts with a post format of “audio”
- Display Current Post’s Position in Custom Taxonomy
- Using Wp_Query without the loop?
- WP Query with multiple tags by get the tags
- Woocommerce pagination and result count after custom loop
- Include latest author posts with pagination in single template?
- How to querry for an item that saved in an array?
- Is it possible to add an argument to a custom function added to a filter hook?
- get_post() is not returning correct value
- Order by meta_key doesn’t work
- CPT category post/content is not showing on my page, Please review my coede
- how to get category`s slug in WP_Query loop?
- Advise on Templates for Custom Queries
- Query WP Page for Buddypress Group Home [closed]
- Creating a query that get all posts but places meta items first
- How to separate two columns from one content on a page template?
- How to wp_query every Sunday between a startdate and enddate?
- WordPress rewrite with custom parameter doesn’t work
- How can I order a post query’s results based on the number of matching taxonomy terms?
- wp query to use both author id and meta_query
- Why last row deleted when refresh page
- wp_query with meta_query and tax_query
- WordPress live search and filter custom fields
- Avoid removing duplicate posts
- Get post Number with local loop and template
- WP_Query tax_query only returning posts that match first term
- Get post related to current post
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- WP query retrieve the src of attached image
- How to set up pagination for a custom loop on a global template (author.php, tag.php, archive.php?)
- Featured Image as Background with Offset
- custom WP_Query results in showing same posts in all pagination pages
- counting post from a wp-query using sticky_post
- Show recent posts from excluded category
- List posts of terms but exclude one term
- Hiding all posts/products/pages from a site based on a custom taxonomy/domain name
- Custom search (wp query by custom fields)
- Passing query variables to a custom page template
- Query child’s child categories
- $wp_query issue when trying to create a virtual page
- add_query_vars does not work
- Retrieve posts in custom post type and specific taxonomies
- Why is WP_Query’s meta_query not filtering results?
- Query specific number of posts for each post type in specific order
- Avoiding page loop
- Query Users by post count, last 30 days and display each users post count according to post type
- how do I exclude child categories from a wp_query?
- how to show only catergory titles on a page in wordpress
- Using ‘meta_query’ with the ‘pre_get_posts()’ hook disables searching for post titles
- Woocommerce set loop_shop_columns to be 3 in the main shop page loop only and 4 otherwise
- How to save the results of a query as a php file for an autocomplete search bar