How to Exclude Posts with a Word from WordPress Query

3.6666666666667 from 5
from 3 user
(Rate this post)
How to Exclude Posts with a Word from WordPress Query

Excluding posts by a word in the title from the WP posts query has a simple solution which I shared in this tutorial. This solution uses the `s` argument in the posts query, and it is easier than what you can imagine. Read this short WordPress tutorial to learn how developers exclude posts from queries by specifying a keyword in the title.

These kinds of functionalities are the main reason that I Love WP 🙂

If you are looking for a way to query WordPress posts, you better first read my tutorial on how to query WordPress posts. Then you can continue this tutorial to learn how to exclude posts from a query by specifying a keyword.

Exclude WordPress Posts from a Query by Keyword

use 's' => '-title' in your arguments when you’re executing the get_posts function (replace title with your desired keyword). This way, you tell the WP to exclude all those posts from the get_post query.

$keywordToExclude = "change keyword here";
$args = array(
	's' => '-'.$keywordToExclude
);

//query posts
$posts = get_posts($args);

//print all posts without the keyword in title
foreach ($posts as $post){
	echo "Title: ".$post->post_title." 
"; }

What do you think about "How to Exclude Posts with a Word from WordPress Query"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu