Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1416999

    I am trying to exclude posts (woocommerce products) which have a specific category (id=340) from the ‘avia_ajax_search’ popup.
    have copied the function if(!function_exists(‘avia_ajax_search’))

    from /enfold/functions-enfold.php to /enfold-child/functions.php
    Is this correct, or do i need a seperate /enfold-child/functions-enfold.php

    
    if(!function_exists('avia_ajax_search'))
    {
    	//now hook into wordpress ajax function to catch any ajax requests
    	add_action( 'wp_ajax_avia_ajax_search', 'avia_ajax_search' );
    	add_action( 'wp_ajax_nopriv_avia_ajax_search', 'avia_ajax_search' );
    
    	function avia_ajax_search()
    	{
    	    global $avia_config;
    		
            $exclude_category_id = 340; // Replace with the actual category ID
    
    	    unset($_REQUEST['action']);
    	    if(empty($_REQUEST['s'])) $_REQUEST['s'] = array_shift(array_values($_REQUEST));
    		if(empty($_REQUEST['s'])) die();
    
    	    $defaults = array('numberposts' => 5, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false, 'results_hide_fields' => '');
    
    	    $_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
    
    	    $search_parameters 	= array_merge($defaults, $_REQUEST);
    
    	    if ( $search_parameters['results_hide_fields'] !== '' ) {
                $search_parameters['results_hide_fields'] = explode(',', $_REQUEST['results_hide_fields']);
            }
            else {
                $search_parameters['results_hide_fields'] = array();
            }
    
            $search_query 		= apply_filters('avf_ajax_search_query', http_build_query($search_parameters));
    	    $query_function     = apply_filters('avf_ajax_search_function', 'get_posts', $search_query, $search_parameters, $defaults);
    	    $posts		= (($query_function == 'get_posts') || !function_exists($query_function))  ? get_posts($search_query) : $query_function($search_query, $search_parameters, $defaults);
    	
    	    $search_messages = array(
    	            'no_criteria_matched' => __("Sorry, no posts matched your criteria", 'avia_framework'),
    	            'another_search_term' => __("Please try another search term", 'avia_framework'),
    	            'time_format'         => get_option('date_format'),
    	            'all_results_query'   => http_build_query($_REQUEST),
    	            'all_results_link'    => home_url('?' . http_build_query($_REQUEST)),
    	            'view_all_results'    => __('View all results','avia_framework')
                );
    		
    	    $search_messages = apply_filters('avf_ajax_search_messages', $search_messages, $search_query);
    		
    	    if(empty($posts))
    	    {
    	        $output  = "<span class='av_ajax_search_entry ajax_not_found'>";
    	        $output .= "<span class='av_ajax_search_image ".av_icon_string('info')."'>";
    	        $output .= "</span>";
    	        $output .= "<span class='av_ajax_search_content'>";
    	        $output .= "    <span class='av_ajax_search_title'>";
                	$output .= $search_messages['no_criteria_matched'];
    	        $output .= "    </span>";
    	        $output .= "    <span class='ajax_search_excerpt'>";
                	$output .= $search_messages['another_search_term'];
    	        $output .= "    </span>";
    	        $output .= "</span>";
    	        $output .= "</span>";
    	        echo $output;
    	        die();
    	    }
    
    	    //if we got posts resort them by post type
    	    $output = "";
    	    $sorted = array();
    	    $post_type_obj = array();
    	    foreach($posts as $post)
    	    {
    	        $sorted[$post->post_type][] = $post;
    	        if(empty($post_type_obj[$post->post_type]))
    	        {
    	            $post_type_obj[$post->post_type] = get_post_type_object($post->post_type);
    	        }
    	    }
    
    	    //now we got everything we need to preapre the output
    	    foreach($sorted as $key => $post_type)
    	    {
    
    	        // check if post titles are in the hidden fields list
    	        if ( ! in_array('post_titles', $search_parameters['results_hide_fields'] ) )
    	        {
                    if(isset($post_type_obj[$key]->labels->name))
                    {
                        $label = apply_filters('avf_ajax_search_label_names', $post_type_obj[$key]->labels->name);
                        $output .= "<h4>".$label."</h4>";
                    }
                    else
                    {
                        $output .= "<hr />";
                    }
                }
    
    	        foreach($post_type as $post) {
    
    	            $image = "";
                    $extra_class = "";
    
                    // Get the categories for the post
               		$post_categories = wp_get_post_categories($post->ID);
    
               		// Check if the excluded category is in the post categories
    	            if (!in_array($exclude_category_id, $post_categories)) {
    
    	                // check if image is in the hidden fields list
    	                if (!in_array('image', $search_parameters['results_hide_fields']))
    	                {
    	                    $image = get_the_post_thumbnail($post->ID, 'thumbnail');
    	                    $extra_class = $image ? "with_image" : "";
    	                    $post_type = $image ? "" : get_post_format($post->ID) != "" ? get_post_format($post->ID) : "standard";
    	                    $iconfont = $image ? "" : av_icon_string($post_type);
    
    	                }
    
    		            $excerpt     = "";
    
    	                // check if post meta fields are in the hidden fields list
    	                if ( ! in_array('meta', $search_parameters['results_hide_fields'] ) )
    	                {
    	                    if(!empty($post->post_excerpt))
    	                    {
    	                        $excerpt =  apply_filters( 'avf_ajax_search_excerpt', avia_backend_truncate($post->post_excerpt,70," ","...", true, '', true) );
    	                    }
    	                    else
    	                    {
    	                        $excerpt = apply_filters( 'avf_ajax_search_no_excerpt', get_the_time( $search_messages['time_format'], $post->ID ), $post );
    	                    }
    	                }
    
    		            $link = apply_filters('av_custom_url', get_permalink($post->ID), $post);
    
    		            $output .= "<a class ='av_ajax_search_entry {$extra_class}' href='".$link."'>";
    
    		            if ($image !== "" || $iconfont) {
    	                    $output .= "<span class='av_ajax_search_image' {$iconfont}>";
    	                    $output .= $image;
    	                    $output .= "</span>";
    	                }
    		            $output .= "<span class='av_ajax_search_content'>";
    		            $output .= "    <span class='av_ajax_search_title'>";
    		            $output .=      get_the_title($post->ID);
    		            $output .= "    </span>";
    		            if ($excerpt !== '') {
    	                    $output .= "    <span class='ajax_search_excerpt'>";
    	                    $output .=      $excerpt;
    	                    $output .= "    </span>";
    	                }
    	          	}
    	            $output .= "</span>";
    	            $output .= "</a>";
    	        }
    	    }
    
    	    $output .= "<a class='av_ajax_search_entry av_ajax_search_entry_view_all' href='".$search_messages['all_results_link']."'>".$search_messages['view_all_results']."</a>";
    
    	    echo $output;
    	    die();
    	}
    }
    
    #1417118

    Hey wusatiuk,

    Thank you for the inquiry.

    You can use the “avf_ajax_search_query” filter instead of copying the whole function. Please add the following code in the functions.php file.

    if ( ! function_exists( 'avf_filter_ajax_search_results_woo' ) )
    {
    	add_filter('avf_ajax_search_query', 'avf_filter_ajax_search_results_woo', 10, 1);
    	function avf_filter_ajax_search_results_woo( $search_parameters )
    	{
    		$tax_query = array(
                         array(
                            'taxonomy'  => 'product_cat',
                            'field'     => 'term_id',
                            'terms'     => array(340),
                            'operator'  => 'NOT IN'
                        )
                    );
    		$defaults = array('numberposts' => 5, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false, $tax_query);
    		$_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
    		$search_parameters = array_merge( $defaults, $_REQUEST );
    		return $search_parameters;
    	}
    
    }
    

    Best regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.