Multisite Search

This topic contains 7 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 4 months, 4 weeks ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #37582
    Sittingduck
    Sittingduck
    Participant

    Hi,
    I would like to find out, is there some way to make a multisite search within a specific category only?
    Thanks

    #37601
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    I’m afraid that would not work, because on each subsite the category IDs are different. I can suggest adding a category restrition programmatically, but if you are searching more than 1 subsite, it will try to get results from that category ID on all of them, and since that ID does not exist (or belong to another category) on the subsites, the results will be inconsistent and missing.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #37602
    Sittingduck
    Sittingduck
    Participant

    Hi,
    Thank you.
    Well actually all subsites will be only searching the main site and not each other. So we basically have company listings on the main site, and we have subsites based on country. So i would want the USA subsite to only search the main site within the USA category. Would this be possible?
    Thanks so much!

    #37624
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    That actually might be possible via a custom code snippet. All you need to know is the blog ID and the taxonomy term ID for which you want to restrict the results to.

    I have constructed a small snippet, which should do that, you only need to plug the values to the $include variable. On the left side is the blog ID, on the right side is the category to include the results for that blog.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter("asp_query_args", "asp_query_args_change", 10, 2);
    function asp_query_args_change($args, $search_id) {
    	$include = array(
    		// Blog ID => Taxonomy term ID
    		1 => 11,
    		2 => 22,
    	);
    	if ( isset($include[get_current_blog_id()]) ) {
    		$args['post_tax_filter'] = array(
    			array(
    				'taxonomy'  => 'category',
    				'include'   => array($include[get_current_blog_id()]), 
    				'exclude'   => array(),
    				'allow_empty' => false // Don't allow results, which does not have connection with this taxonomy
    			)
    		);
    	}
      return $args;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #37626
    Sittingduck
    Sittingduck
    Participant

    Awesome! Thank you so much! i will try it and let you know if it worked.

    #37632
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You are very welcome 🙂

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #37703
    Sittingduck
    Sittingduck
    Participant

    Hi,

    Thanks so much for the code, but unfortunately it does not seem to be working. Im not getting any errors but its still showing other categories.

    I did minor changes to the code:

    add_filter("asp_query_args", "asp_query_args_change", 10, 2);
    function asp_query_args_change($args, $search_id) {
    	$include = array(
    		// Blog ID => Taxonomy term ID
    		1 => 269
    		
    	);
    	if ( isset($include[get_current_blog_id()]) ) {
    		$args['post_tax_filter'] = array(
    			array(
    				'taxonomy'  => 'listing_location',
    				'post_type' => 'listing',
    				'include'   => array($include[get_current_blog_id()]), 
    				'exclude'   => array(),
    				'allow_empty' => false // Don't allow results, which does not have connection with this taxonomy
    			)
    		);
    	}
      return $args;
    }

    i think i did it right based on this: wp-admin/edit-tags.php?taxonomy=listing_location&post_type=listing

    And this is the link to that category: search/?postType=listing&listing_location=268

    Any help would be greatly appreciated.

    Thanks so much!

    #37721
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I may need to test this directly. Can you add temporary FTP and back-end access?

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.