Forum Replies Created

Viewing 15 replies - 1 through 15 (of 23 total)
  • Thread Starter Kamalpreet Singh

    (@kamalpreet11)

    I opened the ticket on your website, thanks

    Yes I defined 3 parameters.
    Thanks

    Like below:

    function search_in_custom_fields_restriction($match){
    		 global $wp_query;
    		  if ( $wp_query->query_vars['exclude_tabbed_content']  == 'yes'){    
    		    $cf     = json_decode( $match->customfield_detail );
    		    $tabbed_content_hits = 0;
    		    foreach ( (array) $cf as $key => $value ) {
    		      if ( strpos($key, "tab_content") !== false ) {
    		        $tabbed_content_hits += $value;
    		      }
    		    }
    		    if ( $tabbed_content_hits == $match->tf ) {
    		      $match->weight = 0;
    		    }else{
    		    	$match->tf -= $match->customfield;
    				$match->weight = $match->tf * $idf;
    		    }
    		  }
    		  return $match;
    	}

    Hi @msaari ,

    The code above is working fine. It’s successfully excluding the custom field as intended. However, I need to figure out how to also reduce the relevance score. Let me give you an example: if someone searches for the phrase ‘some text,’ and this phrase is only in the custom field, the code currently removes the post from the search by setting the weight to 0.

    Now, if this phrase is in the post’s title, content, and the custom field, the post won’t be removed from the search results, which is the correct behavior. However, what I’d like to do is reduce the relevance score of the custom field search as well.

    Is this something that can be done?

    I understand now. The solution provided is indeed working perfectly. Please disregard my previous message. I’ve noticed that we’re adjusting the weight to 0 only if the search term is exclusively found in the custom field. This support has been fantastic. ??

    We’ll proceed with purchasing the plugin. Additionally (Just a suggestion), it would be wonderful if you could consider adding this solution to your website. This way, anyone in need of a similar solution can easily access it.

    Thank you very much!

    The following is the code changes, as the function str_ends_with you described is not functioning as expected. Consequently, the issue arises where $tabbed_content_hits == $match->tf does not match.

    The $match object is presented as follows, and you can observe that the value of tf in this is 21. However, the value of the $tabbed_content_hits variable, which is calculated later, is 6. As a result, these values do not match and not excluding the custom field.

    Have you correctly matched the variable?

    stdClass Object ( [doc] => 9170 [term] => neck [term_reverse] => kcen [content] => 6 [title] => 1 [comment] => 0 [tag] => 0 [link] => 0 [author] => 0 [category] => 0 [excerpt] => 2 [taxonomy] => 0 [customfield] => 8 [mysqlcolumn] => 0 [taxonomy_detail] => [customfield_detail] => {"position":2,"tabbed_content_2_tab_content":1,"tabbed_content_3_tab_content":4,"tabbed_content_4_tab_content":1} [mysqlcolumn_detail] => [type] => post [item] => 0 [tf] => 21 [taxonomy_score] => 0 [weight] => 7557.476669391 )
    add_filter( 'relevanssi_match', function( $match ) {
      global $wp_query;
      if ( $wp_query->query_vars['exclude_tabbed_content']  == 'yes'){    
        $cf     = json_decode( $match->customfield_detail );
        $tabbed_content_hits = 0;
        foreach ( (array) $cf as $key => $value ) {
          if ( strpos($key, "tab_content") !== false ) {
            $tabbed_content_hits += $value;
          }
        }
        if ( $tabbed_content_hits == $match->tf ) {
          $match->weight = 0;
        }
      }
      return $match;
    } );

    Okay, these are the main technical terms that we need to fully work with the Relevanssi premium plugin. Below is the query we want to run. Please review these points:

    1. As you can see, we have multiple tax queries with the ‘lesson_attributes’ taxonomy using the ‘AND’ operator. It should work properly. In the simple wp_query, it is functioning correctly. As the Relevanssi plugin modifies the wp_query and executes it in its own way. Therefore, this tax query should work.
    2. We need to exclude the post_content based on a condition, so that the code will not search the specified string within the post_content. (You’ve already provided the idea for this, and it’s working great.)
    3. We also need to search the text within an ACF (Advanced Custom Fields) repeated field. I understand that in the premium version, there is a setting in the backend where we can define the weight of each custom field. We require a function that allows us to exclude searching within this repeated field, similar to how we exclude searching in the post_content. As per my understanding we need to add some code and define the weight for ‘tabbed_content_%_tab_content’ to 0 like we did for ‘post_content’, correct?

    @msaari Please confirm these points, and once confirmed, we will proceed to purchase the premium plugin.

    Thank you for your assistance thus far.


    $args = array(
        'post_type' => 'lesson',
        'post_status' => 'publish',
        'orderby' => 'relevance', // Sort by relevance
        'order' => 'DESC', // Sort order
        's' => 'neck', // Search keyword
        'posts_per_page' => -1, // Retrieve all posts
        'relevanssi' => true,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'lesson_cat',
                'field' => 'term_id',
                'terms' => array(11,54,22,21,12),
                'operator' => 'IN',
            ),
             array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(56,57),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(58,59,60),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(55,69),
                'operator' => 'IN',
            ),
        ),
        
    );

    Yes it is difficult to exclude the acf repeated field from search as well.

    Excluding content is working great, thanks.

    I am bit confused with excluding search from custom fields. I have two custom fields
    notes
    tabbed_content_%_tab_content

    In one case I have to exclude ‘notes’, in one case I have to exclude ‘tabbed_content_%_tab_content’

    Just let me know where should I set the weight for post_content and custom fields (repeater and normal). In wp_query arguments? Can you please give me an example code?

    We will see if we need the preminum plugin.

    No, I don’t want it to search anywhere in the post. Just in the fields I will define. In some cases I don’t want it to search in post_content. In some cases I do want it to search in some other post meta fields. All these search should be conditional based.

    I will try this. Will this works like this:

    Search in post_title OR post_content OR post_custom_fields OR more custom_fields?

    Regarding the meta query, I’m using custom SQL precisely because it’s not feasible with the wp_query to search meta fields using the LIKE operator and simultaneously apply conditions on the meta values.

    Using the ‘visible custom fields’ option isn’t viable for us due to the number of fields. We’re aiming to search in specific fields that meet certain conditions.

    We’re utilizing ‘lesson_attributes’ multiple times because our search form incorporates three dropdowns, each representing distinct ‘lesson_attributes’. Consequently, our search needs to retrieve posts associated with multiple attributes based on the filter criteria.

    Actually we have a form build for users, where they can select the ‘lesson_cat’ and lesson_attributes from the dropdown. that is why we need to include this in the query. We can’t search in all categories, we have to search only in those categories or attributes that user selects in the form.

    Actually What we need is: Get all posts where post_title is LIKE ‘some text’ OR post_content is LIKE ‘Some text’ OR post_custom field is LIKE ‘tabbed_content_%_tab_content'(as this is a repeated field with ACF) and meta_value is LIKE ‘some text’. The relationship with the post_title, post_content, and post_custom fields is OR.
    And these posts should be present in some categories.

    So we tried this with wp_query. It is rerun nothing. Also, we define the custom fields in the plugin settings as well.
    See this: https://prnt.sc/-hCyfeTZwNfX

    See the wp_query we are trying:


    $args = array(
        'post_type' => 'lesson',
        'post_status' => 'publish',
        'orderby' => 'relevance', // Sort by relevance
        'order' => 'DESC', // Sort order
        's' => 'neck', // Search keyword
        'posts_per_page' => -1, // Retrieve all posts
        'relevanssi' => true,
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'tabbed_content_%_tab_content',
                'value' => 'neck',
                'compare' => 'LIKE'
            ),
        ),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'lesson_cat',
                'field' => 'term_id',
                'terms' => array(11,54,22,21,12),
                'operator' => 'IN',
            ),
             array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(56,57),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(58,59,60),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'lesson_attributes',
                'field' => 'term_id',
                'terms' => array(55,69),
                'operator' => 'IN',
            ),
        ),
        
    );
Viewing 15 replies - 1 through 15 (of 23 total)