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."
";
}
written by Mehdi Nazari about in WordPress WordPress Plugin Development WordPress Theme Development
What do you think about "How to Exclude Posts with a Word from WordPress Query"?