limit to parent for each search instance

Home Forums Product Support Forums Ajax Search Pro for WordPress Support limit to parent for each search instance

This topic contains 1 reply, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 week, 1 day ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39302
    23volt
    23volt
    Participant

    Here i learn how to limit results to specific posts:

    https://knowledgebase.ajaxsearchpro.com/miscellaneous/post-types/limiting-results-to-specific-posts-by-parent-id
    Now i wanna limit search instance id 1 to parent post id: 1105 and search instance id 2 to parent post id: 1105.
    My question is:
    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(1105);
    /**
    * Search instance IDs you want this code to apply on.
    */
    $search_ids = array(1);

    // ————————————————–
    // ————————————————–
    // — !! Do not change anything below this line !! —
    // ————————————————–
    if ( in_array($id, $search_ids) )
    $args[‘post_parent’] = $ids;

    return $args;
    }

    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(1106);
    /**
    * Search instance IDs you want this code to apply on.
    */
    $search_ids = array(2);

    // ————————————————–
    // ————————————————–
    // — !! Do not change anything below this line !! —
    // ————————————————–
    if ( in_array($id, $search_ids) )
    $args[‘post_parent’] = $ids;

    return $args;
    }

    But its not working. How i can have this please?
    search instance 1 need to be limited to parent 1105 and search instance 2 need to be limited to parent 1106.

    Thank you for your so great plugin and work on it!
    Tommy

    #39304
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Thank you for the details, it helps me a lot!

    Using multiple isntances of that code will not work, it will yield an error. There is a much simpler way, of adding an if-else statement to check the search IDs:

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    	if ( $id == 1 ) {
    		$args['post_parent'] = array(1105);
    	} else if ( $id == 2 ) {
    		$args['post_parent'] = array(1106);
    	}
    	return $args;
    }

    This will add the post parent arguments depending on the search ID.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.