admin管理员组文章数量:1122846
Using the standard Wordpress search syntax, I'm trying this:
/?s=-2024&category_name=forsale
My goal is for this search to remove posts that include "2024" in the title.
But as far as I can tell, this is actually filtering out any post from the year 2024 regardless of whether it has "2024" in the title or not.
How can I get the (-) syntax to remove purely by title?
If this were Google I would use something like -intitle:"2024"
but no luck here.
Using the standard Wordpress search syntax, I'm trying this:
https://sample.wordpress.com/?s=-2024&category_name=forsale
My goal is for this search to remove posts that include "2024" in the title.
But as far as I can tell, this is actually filtering out any post from the year 2024 regardless of whether it has "2024" in the title or not.
How can I get the (-) syntax to remove purely by title?
If this were Google I would use something like -intitle:"2024"
but no luck here.
- 1 Your sample URL refers to wordpress.com. If your site's hosted on wordpress,com, you'll need to check with their support team; any solutions involving custom code, plugins, etc, may not work (or may require a paid tier to work). – Pat J Commented Jun 18, 2024 at 15:07
2 Answers
Reset to default 1- Using the "NOT" Operator (Limited Functionality):
Unfortunately, WordPress search doesn't have a dedicated "NOT" operator like Google. However, there's a workaround using the tax_query parameter that might achieve a similar result (but with limitations):
Your current search URL is: https://sample.wordpress.com/?s=-2024&category_name=forsale Try modifying it to: https://sample.wordpress.com/?s=&tax_query=[{"taxonomy":"post_tag","field":"slug","terms":["2024"],"operator":"NOT IN"}]&category_name=forsale Explanation:
This modified URL uses the tax_query parameter to target posts with tags. We set the taxonomy to "post_tag" (assuming you use tags for year classifications). We search for the slug "2024" (the year) but use the operator as "NOT IN". This should ideally exclude posts with the "2024" tag (representing the year). Limitations:
This method only works if you consistently use tags for year classifications. It won't exclude posts where "2024" appears in the title but not as a tag. 2. Using Plugins for Enhanced Search:
Several WordPress plugins offer more advanced search functionalities. Some popular options include:
Relevanssi: Provides advanced search features like excluding specific terms, searching by custom fields, and weighting search results. SearchWP: Offers similar capabilities to Relevanssi, including boolean operators like "NOT" and searching by post meta information. These plugins typically provide a user-friendly interface to configure your search parameters, allowing you to exclude posts based on specific criteria like titles containing "2024".
- Custom Search Code (For Developers):
If you're comfortable with code, you can modify WordPress core search functionality using a custom plugin. This approach gives you complete control over the search logic but requires development expertise.
To specifically exclude posts with "2024" in their titles using a code snippet, you can add this to your theme’s functions.php
file:
function exclude_title_posts( $query ) {
if ( $query->is_search && !is_admin() ) {
$query->set('post_title_not_like', '2024');
}
}
function modify_search_where( $where, $query ) {
global $wpdb;
if ( $title_not_like = $query->get( 'post_title_not_like' ) ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title NOT LIKE %s ", '%' . $wpdb->esc_like( $title_not_like ) . '%' );
}
return $where;
}
add_filter( 'posts_where', 'modify_search_where', 10, 2 );
add_action( 'pre_get_posts', 'exclude_title_posts' );
This snippet modifies the WordPress query when it's a search query, adding a condition to exclude posts whose titles contain "2024".
本文标签: urlsHow to search Wordpress for content specifically in title
版权声明:本文标题:urls - How to search Wordpress for content specifically in title? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303414a1931878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论