Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Mahfuzul Alam

    (@mahfuz87)

    Hello

    Thank you for asking. Could you please explain a bit more with an example? I will try to help you with this matter.

    Regards

    Thread Starter Fabio Nogueira

    (@fan297)


    I need to search for a listing for something that is unique, using the listing title can result in an error as it is a field that changes depending on typing. In this sense the listing ID would work well. I was even able to display the listing ID on the page, but I was unable to create a search field to find the listing by that ID.

    Plugin Support Mahfuzul Alam

    (@mahfuz87)

    Hi @fan297

    Thank you for waiting on this.

    Please use the following code in the functions.php file of the child theme file. This will allow you to search with the listing ID as well.

    add_filter( 'atbdp_listing_search_query_argument', function( $args ) {
    	
    	if (isset($_GET['q']) && !empty($_GET['q'])) :
    		$post_per_page = $args['posts_per_page'];
    		$args['posts_per_page'] = -1;
    		$args1 = $args;
    		$args1['fields'] = 'ids';
    		$post_args1 = new WP_Query($args1);
    		$posts1 = !empty($post_args1->have_posts()) ? $post_args1->posts : [];
    
    		$args2 = $args;
    		$args2['p'] = (int)$args2['s'];
    		unset( $args2['s'] );
    
    		$args2['fields'] = 'ids';
    		
    		if( $args2['p'] ){
    			$post_args2 = new WP_Query($args2);
    			$posts2 = !empty($post_args2->have_posts()) ? $post_args2->posts : [];
    		}else{
    			$posts2 = [];
    		}
    		$all_ids = array_merge($posts2, $posts1);
    
    		if (!empty($all_ids)) {
    			$args['post__in'] = $all_ids;
    			unset( $args['s'] );
    		} else {
    			$args['s'] = $_GET['q'];
    		}
    		$args['posts_per_page'] = $post_per_page;
    	endif;
    	
    	return $args;
    } );

    Please let me know if you need further assistance.

    Kind Regards

    Thread Starter Fabio Nogueira

    (@fan297)

    Worked perfectly. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search field by the listing page id’ is closed to new replies.