If you are altering main query then use pre_get_posts
filter instead of WP_Query
.
add_action('pre_get_posts','wpse224610_alter_query');
function wpse224610_alter_query($query){
$cat_id = get_cat_ID('Featured articles');
$exclude_cat_id = -$cat_id;
if( !is_admin() && $query->is_main_query() ){
$query->set( 'cat', $exclude_cat_id );
$query->set( 'posts_per_page', 4 );
}
}
And the main loop will be
if( have_posts() ):
while( have_posts() ): the_post();
//The loop
endwhile;
endif;
Check the return value of $cat_id
, is it 0 or Featured articles category id ? check whether you spelled category name correctly as it is in the admin ?
From your code i assume you are trying to do secondary loop but as you told it’s main loop , use above mentioned filter.
Related Posts:
- WP_Query with “post_title LIKE ‘something%'”?
- How to extend WP_Query to include custom table in query?
- compare meta_query in get_posts arguments
- How to use the_posts_navigation for wp_query and get_posts?
- How to do a wp_query using “BETWEEN” with two meta_values?
- How do I order pages and categories by ID or name in the same query?
- How to do a query only on a specific admin page?
- WP Cron and wp_insert_post
- How to adjust found_posts so that it accounts for offset and pagination
- SQL query equivalent to WP User Query
- Function to check if author has posted within the last x days
- Single page theme that uses pages for the content
- Show default content if custom WP_Query has no posts
- WP Query for Posts (Products) in Specific Category that has 2 Specific Tags (*AND* both tags not *OR*)
- meta_query on a date range using an array of values
- Pagination for custom query on single.php
- wp_transients | wp_object_cache VS SESSIONS & Cookies?
- Get product list of given category
- Remove ellipsis from the excerpt retrieved using get_the_excerpt()
- WordPress query vars are not added
- When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?
- How to prevent writing duplicate loops?
- Taxonomy search/filter with multiple taxonomies and multiple taxonomy terms
- Alternative to query->set that appends conditions instead of overriding
- Group WP_Query by meta_key date
- Set if condition with wp_nav_menu
- why update and delete query not worked in custom table?
- get_users is missing or skipping over users in loop
- WP_Query sorted by custom taxonomy
- how to handle multiple ajax wordpress queries?
- problem by deleting new plugin installed inside the WordPress after any heroku app sleeping [closed]
- Use posts_groupby filter with part of string in wp_query?
- Adding multiple meta_key fields as orderby options to a WP Query via a function
- 1/3 of posts different class
- wp_Qwery works to slow
- Search for “2nd” also returns “22nd” in wp_query
- How to output the title of the blogs home page
- Get posts by list of post IDs ordered by those IDs?
- WordPress Blog Posts with Pagination inside a Page/Post
- Reset WordPress Post Query to default
- Woocommerce – exclude only older out-of-stock items
- Limit home post to 10 without creating pagination?
- Searching for meta_key returns 0 posts
- Using custom taxonomies in a query
- ACF accessing a field from the query, but not in post
- What is the best way (regarding performance) to set transients for logged in users?
- List of ways to access WordPress database?
- WP_Query Meta_key is text value and need to sort as numeric not working
- Is there a ‘compare’ option when using WP_Query apart from meta_query
- WP_Query Results Issue with ACF
- Create a page template for “top rated posts” but show full content and not just a list
- Why tax_query in WP_Query not working
- Differentiate Nested WP_Query from Parent
- WP_User_Query and user posts
- Add URL Rewrite Rule To WordPress
- WP order ASC in custom author loop
- localizing variable on front-page.php template fails but succeeds on single page and taxonomy archive
- My website is getting too many dierect home arechives and this is increasing my bounce rate
- Remove from array in WP_Query loop
- How can I detect if the current post is in this loop?
- Posts limit on homepage (genesis framework)
- wpquery via ajax
- Trouble passing attribute into shortcode function
- Where is this query?
- WP_Query return posts in wrong order
- Exclude post with taxonomy and from the terms (taxonomy & tag) posts count
- WordPress search WP_Query to cover multiple post types and their custom fields?
- How do i create a custom post query when the meta value is an array?
- How to merge multiple foreach into 1 foreach and sort by date
- posts_per_page not working in block
- Why does my output of get_the_ID() change after a wp_query?
- Very slow query generated getting meta data from posts
- Ajax buttons not working properly in WooCommerce when using wc_get_template_part
- How to query 5 users in random who have published more than 10 posts
- Limit default Search query to post_title
- in tax_query this Is the code correct? I need to access beginner video posts for different subject
- WP Query works outside a function, not inside a function
- WP_Query returns images but post_per_page is incorrect
- Problem with custom WP_Query and underlying pagination/posts_per_page
- Pull posts from all categories if quantity is not met?
- 3 wp_query on one page with pagination for last query
- Custom pagination (Title, date and teaser)
- Sorting Posts with meta value not working
- Display posts from catagories
- Fire query on ajax post url page
- What is wrong with this pagination code?
- Read more redirection problem
- Multiple loops on index page with sticky post and pagination
- Randomly display posts on a site hosted by WPEngine? [closed]
- tax_query not working properly with get_posts
- how to add limit records in wordpress query
- query only direct child and sub-terms of a current term archive
- Get access to all terms associated to each post that the wp_query loop displays
- WP_Query by meta key not returning any posts
- Use value from meta key array for use in WP_Query
- WP_Query sort by ACF date field (newest first) with blank dates first
- Pagination only showed when no category is set in wp_query
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Prevent URL Parameter Affecting other WP Query