You can first get posts from your featured category and check if it has any posts or not. If yes, you will show them.
If not, you will make another query that will get 3 posts from random categories (as you described) and show them.
$events_args = array(
// include the code in your question here
);
$events = new WP_Query( $events_args );
if ($events->have_posts()){
while ($events->have_posts()) : the_post();
// your loop code here
endwhile;
}
if ((3-($events->found_posts)) > 0) {
wp_reset_postdata();
$remaining = 3 - ($events->found_posts);
$random_args = array(
// your desired array of attributes, set 'posts_per_page' to $remaining
);
$random_posts = new WP_Query($random_args);
while ($random_posts->have_posts()) : the_post();
// your loop code here
endwhile;
}
Related Posts:
- Quickest way to get last or oldest post date – WP Query
- How to get post content by calling ajax?
- Get the ID of the latest post
- Retrieving 3 latest post from each of 5 different custom post types
- Wp-query causing problems with the_content();
- How to create posts (not post template) to be displayed on projects page?
- How do I create an archive page for standard posts?
- Why does using WP_Query inside a shortcode in an elementor page cause the arguments for WP_Query to get malformed?
- Loop on front-page.php
- WP_Query is printing out only one post when posts_per_page is set to multiple
- WordPress Orderby Numeric Value Not Working
- is there a way to retrieve posts that do not have a featured image assigned?
- Shortcode for display posts on home page show only one post
- How to add post_type=value when editing that post type in the WordPress admin?
- Storing posts from query and accessing later via AJAX call
- Show custom post type filtered by category
- Get latest 3 posts from multiple CPT in one query
- Stuck in Order by more then one
- Query Multiple Custom Posts by Custom Fields
- Several post types on WP Query by tag and taxonomy
- Prioritize posts in query by meta keys?
- How to get all tags of a custom post type by id
- WP the_posts() on single-cars.php get category link
- Different Limit number of post on different archive page
- Reworking function for counting custom post type posts count
- Custom post types and ‘new WP_Query’
- Variable not working in WP_Query
- How to Make infinite loop of post
- WP_Query with custom post type ID
- Query within a foreach within a query (queryception)
- wordpress lists similar type of posts in a custom post type
- display posts of custom post type with custom taxonomy
- How to Query in WordPress which shows Alphabetic Posts?
- Group by custom field value (start and end times)
- How to query Posts from a custom post type which contains a custom taxonomy?
- Display post from custom post type
- Get posts by category name
- list posts of two post types in a single template
- How to properly use Categories with Custom Post Types
- How to fetch posts that are stored in the different table than (default) wp_posts?
- date_query empty results with custom post type
- Get_post_custom not fetching value from array wordpress
- Display Parent-Child Posts in specific order by comparing IDs in array
- Limiting the Number of User Posts to Their Own Posts
- how to display posts content on the custom css popup by clicking on each title on the sidebar?
- wp_post_delete is deleting all offices
- SEARCH QUERIES – REVERSE OUTPUT
- How do I insert a custom post type query after a certain number of recent posts and then resume recent posts?
- Query a custom taxonomy in a function to create an csv file
- minimize wp_query call to database
- subtracting the current post form then whole loop, which is generating all CPT titles
- post type => ‘any’ not applied my custom queries
- Custom Post By Category
- Query posts based on user preferences
- Custom post Query and WordPress Post Query Clash
- How to get the posts that my following users are liked?
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- Most efficient way of showing children posts?
- How can you use a page for a custom post type?
- Only show current category post
- How To Loop Through list with Custom Post Types
- This wp_query will not return any posts and only seems to work with post_status inherit?
- Popup panel is only displaying 1 entry ignoring all others
- Calling specific page with wp query
- How to create content automatically when a post is published?
- Query posts by a type and another type only if post is in specific category
- Retrieving IDs of child pages which are in a different post type
- Query Posts From Multiple Post Types
- Custom Post Type children and grand-children in one list
- shortcode for recent custom type post
- Change display wp post type to wp custom post type
- Dynamically change post_parent in every page load?
- How do I display the index position of a post from a custom post type?
- Cannot exclude particular post from loop of custom post type
- How do I correctly query posts from a post ID?
- Display posts from another page on home page in wp-editor
- How do I hide single category post on my post page
- Wp-theme Development
- show the most recent date of all posts to display on front end
- How to get permalink of post based on post name?
- Sort ACF by custom taxonomy
- Meta query with boolean true/false value
- How to force one column layout on custom post type edit page?
- category__in not working on custom post type
- Date query for a custom meta field
- Allow non-logged in users to see a future post after clicking on a list of future posts
- Update Post Meta for a logged in user
- Problem querying Custom post type by custom fields
- can these 3 queries be re-written as 1 query?
- cannot override post_types in WP_Query()
- Wrapping x posts in html without leaving empty html
- Can’t change posts per page in WordPress Post Type Query
- Invalid content when I try to import custom post type from the old template wordpress
- How to set post date in post_type so that it’s same as latest post in category
- Create a post automatically if search result has zero results
- Bulk trashing post ‘fails’
- Linking posts together with Advanced Custom Fields “both ways”
- get_post_meta for Custom Post Type ( CPT )
- How to query posts by meta keys AND under specific category?
- Front end post or photo or both