query_posts exclude category by id
Lets say I have a post which was ticked under category 12 13. From above code I will not get that post as a result as it is excluding posts under the category 12.The problem with categorynotin is that it produce NOT IN SELECT SQL query. SELECT SQLCALCFOUNDROWS wpposts.ID addaction( pregetposts, mhexcludefeaturedquery ) but im also trying to do the same thing in the category.php, where i would show the latest post tagged as featured from that category. and then the remaining posts below with the featured postiscategory() will accept a category slug or ID. function custompostsonhomepage( query ) if ( query->ishome() query->ismainquery() ) . category getcategorybyslug(produce)But you can use the ID instead if you prefer. The minus sign simply tells WordPress to exclude a category. There are many ways to query posts in WP, however, this article will make use of the common getposts function, WPQuery Object and pregetposts filter.In this category. Add this code to the very end of the file before closing php tag ?> and replace 6 with your category ID: function excludecategory( query ) if ( query->ishome() query->ismainquery() ) query->set( cat, -6 ) addaction( preget posts, excludecategory ) ID) if ( categories ) categoryids array() foreach ( categories as individualcategory ) categoryids individualcategory->termidWordPress Query All Posts To Count How Many Times a Shortcode is Used. function excludecategory( query)query->queriedobject wppa::getpostbytitle(query->query vars[pagename])query->queriedobjectid query->queriedobject->ID errkk/Query to get posts by category - Wordpress( PHP).SELECT posttitle, ID identificativo FROM wpposts.Exclude category slugs, only shows parent cats. To exclude one category posts from homepage return query addfilter(pregetposts, themeprefixexclude category) Replace -11 with your category ID in the above code. In your themes functions.php file, just insert the following code, replacing the category IDs with the ones you want to excludeThe Simply Exclude plugin adds a simply filter to this WPQuery object.
Depending on the Simply Exclude settings for Home, Search, Archive, Feed and is Posts, Pages return query addaction( pregetposts, themeprefixexclude category ) Then add the function inside your functions.php file.So in this example, only the posts in a category id of 3 would show. query new WPQuery( array( categoryname > staffnews ) ) Exclude Posts Belonging to Category. Display all posts except those from a category by prefixing its id with a - (minus) sign.Return posts within the category, but exclude sticky posts completely, and adhere to paging rules To query WordPress and return posts based on these custom taxonomy names and terms, we can useexclude) ) attachments getchildren( array(postparent > id, exclude > excludeI want to show the posts from a specific category in recent work. I have the following code The post ID filter for Views allows to return or exclude one or several posts by their IDs. You can also add the IDs as a URL parameter or an attribute when you add your Views shortcode to a page. In the Query Filter section, click on Add a filter. Customizing the WordPress Query using pregetposts for the main query. Custom WordPress Queries For generating custom queries.DPS Exclude Displayed Posts. This snippet of code hooks into the pregetposts filter, extracts the category ID from the politics slug and excludes it from the search query. Replace the bolded politics with your own category slug.
How can I exclude one specific post from a WPQuery query? (For example, show all posts apart from a post with the ID 278).The first loop displays all posts in a category, and collects the post ids into an array. roborourke/exclude-categories.php. Created Dec 19, 2011.addsettingsfield( excludecategoriesid, ( Exclude categories from main blog ), excludecategoriesfield, reading )return queryquery) 2 if ( query->ishome() ) 3 query->set(cat, -xx) 4 5 return query 6 7 addfilter(pregetposts, excludecategory)from a post you need to change the above code to allcomments wpcountcomments( post->ID) / allcommentsapproved allcomments When ordering by date and excluding a category with the following code: Queryposts( querystring . orderbydateorderASCcat-1 ) The following query is executed: SELECT SQLCALCFOUNDROWS wpposts. ID FROM wpposts WHERE 11 AND ( wpposts.ID NOT pageid getqueriedobjectid() post getpost(pageid) filterargs array( tax query>array(), metaquery>array() ) Email codedump link for Exclude specific taxonomy from query. Email has been send. To emailaddress queryposts( args) exclude a category. Posted by: admin December 31, 2017 Leave a comment.Exclude Posts Belonging to Category. Display all posts except those from a category by prefixing its id with a - (minus) sign. You will use the "Query Category" dropdown or text field to select a category of posts to be shown on the page, or to manually enter the IDs or slugs of categories to be included/excluded from the query. You can read about queryposts in the WordPress Codex here. To use any of these examples you must place the PHP code snippet before the loop.I would like to exclude CERTAIN categories from showing up there, but absolutely cannot figure out how to do this. I know the category id By using positive IDs in the query you can define which categories you want to show in your search results (show categories with IDs 1,2 and 3).You can include or exclude certain post types from search, exclude posts that are older then a specific date, alter the number of items displayed on the query new WPQuery( array( categoryin > array( 5, 6, 7, 8 ) ) ) Show Posts In A Multiple Categories By Name.postparentnotin - Specify a number of post IDs to exclude from the search. query new WPQuery(array(cat > catid., .excludes)) Its technically working, and it pulls in the posts, but then some get filtered out after being added because of the exclusion due to some having the category I want, but are also in a subcategory that gets filtered out. queryposts(categorynameStaff Home) Exclude Posts Belonging to Only One Category. Show all posts except those from a category by prefixing its ID with a - (minus) sign.childcats (array) gettermchildren( getcatid(query->query vars[categoryname]), category )Frank, it seems that you changed the excluded categories array name midway.FROM wpposts INNER JOIN wptermrelationships ON (wpposts.ID How to exclude a post by id from the category.php query in wordpress. 1.The Loop « WordPress Codex. Description:The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according categories getcategories( array(exclude > 1) ) foreach ( categories as category ) include category->termid But I cant figure out why the combination of excluding a category and ordering by date doesnt work when using queryposts. I saw Function Reference/query posts. there is parameter categoryname which displays posts from only one category by name.First get ID using getcatid function then using cat parameter with -(minus sign) and category ID to exclude. Use pregetposts to exclude the categories you dont want to display in the loop. Function excludepostsfromspecificcategory( query ) if ( query->ishome() query->ismainquery() ) . query new WPQuery( array( categoryname > staffnews ) ) Exclude Posts Belonging to Category. Show all posts except those from a category by prefixing its id with a - (minus) sign. An easy way to exclude your current post i.e. the post you are currently viewing, from your Wordpress Query.So, in the code below gettheID is fetching the ID of the current post, cat is indicating the category I want to show posts from, showposts is indicating how many posts I would like to display Excluding pages from the WordPress queryposts loop is easy. You can exclude a page by its ID, Slug or Title. See the example below to get an idea how the WordPress queryposts function can work for you!Categories. 5. Exclude All Pages AND Some Posts by Categories from WordPress Search Results. If you want to search in Posts Only BUT you want to Exclude Specific Posts by category ID.But Exclude certain Posts belonging to a specific Category ID. query ->set(cat,-143) Could anyone explain why this query isnt working? I want to exclude the posts tagged with homepage.As given in the docs in case of excluding categories you have to use its ID and not slug (check here). return query addfilter( pregetposts, excludecategoryhome )You can use Reveal IDs plugin to catch category id. If we want to exclude two categories then you need to add categories ids separated by coma.
Ensure that taxonomy, term, termid, cat, and categoryname vars are set for backward compatibility. / if ( ! empty( this->tax queryIf any posts have been excluded specifically, Ignore those that are sticky. if ( !empty(sticky posts) !empty(q[postnotin]) ) stickyposts WordPress Event Management, Calendars Registration Forums General Question Exclude Categories from wp-query.In this case, posts that are categorized with categories (by ID) 35, 220, and 145 arent displayed. query new WPQuery( categorynamenews,reviews,tutorials ) You can display all posts except those from a certain category by adding a minus sign before the category id you want to exclude. use page id.The default WPQuery sets poststatus>published, but atchments default to poststatus>inherit so youll need to set the status to inherit or any. The cat parameter is straightforward: just use a single category ID or a string of category IDs. Querying for one category looks like thisThe tagnotin parameter lets you query posts which dont have a given tag or array of tags. Use it like this to exclude one tag queryposts(categorynameStaff Home) Show Several Categories by ID. Display posts from several specific category IDsExclude Posts Belonging to Only One Category. Show all posts except those from a category by prefixing its ID with a - (minus) sign. 7. Get Post ID by a Post Title . This is a build-in WordPress function and since 3.0.0 version it works not only for pages but for any custom post type.11. Easy Way to Get Post ID in a WPQuery loop . I want to exclude the posts tagged with homepage. It still shows the post with category n.There are 2 issues in your code. Youre using a slug instead of an ID to exclude a category and you arent using the loop correctly with your custom query. I would like to exclude the current "page" (not post) from a categroy loop I have created 2 categories (archive.php) and releated 2 posts to each category.myquery new WPQuery(array( exclude > excludeids, posttype > post, poststatus > publish query new WPQuery( cat . categoryid ) Exclude Posts In A Certain Category.query new WPQuery( p .postid ) Get Post By Post Slug. return query addfilter(pregetposts,wpexcludecategory) Now with a slight change to the earlier function, this will allow you to exclude a certain category from your site feed. Again, you can specify multiple categories by separating each category ID with a comma.