0

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
  • 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 at 15:07

2 Answers 2

0

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".

0
  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".

  1. 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.