page only specific search

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 4 years, 1 month ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #26083
    mcloughlin01
    mcloughlin01
    Participant

    I have created a search called condo building search. I have started to create pages focused on specific condo buildings.
    I want to use this search to only look in these specific pages.
    all the pages i create i have added under the parent URL structure of /condos/[name of condo building]
    Am i am to make a search that only looks for items within the /condos/ pages?

    #26093
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Well, the plugin supports inclusions exclusion by categories or terms, you can also exclude items by ID or manually, or even by dates or users. (related documentation)

    In this case however it might be better to use a custom coded solution to either restrict the results to specific page IDs, or if the pages have a common parent page, then you can even restrict the results to that.

    Best,
    Ernest Marcinko

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


    #26145
    mcloughlin01
    mcloughlin01
    Participant

    Are you able to show me an exmaple this page “condos”, with the login details i sent you please?
    Im unsure how to implement that? would it search all pages under the /condos parent page?

    #26150
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Sure! The Condos page ID is 7783 as I can see in the page editor. In this case, you need this custom code. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
      /**
       * Enter the post/cpt prent IDs here. The results will be
       * limited to objects with these parent IDs.   
       */     
      $ids = array(7783);
      /**
       * Search instance IDs you want this code to apply on.
       */
      $search_ids = array(1, 2);      
      
      // --------------------------------------------------
      // --------------------------------------------------
      // -- !! Do not change anything below this line !! --
      // --------------------------------------------------
      if ( in_array($id, $search_ids) )
        $args['post_parent'] = $ids;
      
      return $args;
    }

    Make sure to change the $search_ids variable according to which search IDs do you want to restrict to this condos search.

    Best,
    Ernest Marcinko

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


    #26153
    mcloughlin01
    mcloughlin01
    Participant

    thank you!
    is it possible to code for ALL pages under the condo ID page?

    #26162
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I assume you mean non-direct descendants of that page (all child pages).
    Well, I am not sure if that is possible, but this may work instead of the previous code:

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    	$parentid = 7783;
    	$search_ids = array(1, 2);   
    
    	// No change below
    	$child = new WP_Query( array('child_of' => $parentid, 'post_type' => 'page', 'fields' => 'ids) );
    
    	if ( is_array($child->posts) && count($child->posts) >0 && in_array($id, $search_ids) )
    		$args['post_in'] = $child->posts;
      
      return $args;
    }
    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.