Add Booking Requests filter to search page

Hey,

I’ve been using the HivePress AI to add some custom snippets as filters on the listings search page, which seems to have been working okay so far.

However, the following snippet which is supposed to check if the booking requests field is enabled produces results whereby 1 listing that is featured, but does requires manual approval for the Booking Request option is showing up, whilst the rest of the results that appear don’t require approval (which is correct) - So, I deem them as ‘instantly bookable’. (i.e. the featured listing shouldn’t appear here) - Can you please help me exclude the featured listing, unless it is also ‘instantly bookable’. I’m not sure if the Featured listing priority logic is interfering with my attempts.

I’ve got this snippet so far:

add_filter(
    'hivepress/v1/forms/listing_filter',
    function( $form ) {
        $form['fields']['instant_only'] = [
            'label'      => esc_html__( 'Booking Type', 'hivepress' ),
            'caption'    => esc_html__( 'Show only instant-book listings', 'hivepress-bookings' ),
            'type'       => 'checkbox',
            'default'    => false,
            'filterable' => true,
            '_order'     => 31,
        ];

        return $form;
    },
    1000
);

add_action(
    'pre_get_posts',
    function( $query ) {
        if (
            is_admin() ||
            ! $query->is_main_query() ||
            $query->get( 'post_type' ) !== 'hp_listing'
        ) {
            return;
        }

        // Only apply when the filter checkbox is checked
        if ( empty( $_GET['instant_only'] ) ) {
            return;
        }

        $meta_query = $query->get( 'meta_query' );
        if ( ! is_array( $meta_query ) ) {
            $meta_query = [];
        }

        if ( ! isset( $meta_query['relation'] ) ) {
            $meta_query['relation'] = 'AND';
        }

        
        $meta_query[] = [
            'relation' => 'OR',
            [
                'key'     => 'hp_booking_moderated',
                'compare' => 'NOT EXISTS',
            ],
            [
                'key'     => 'hp_booking_moderated',
                'value'   => '1', 
                'compare' => '!=',
            ],
        ];

        $query->set( 'meta_query', $meta_query );
    },
    1000
);

Edit: The ones I really need are filters for verified vendors only, featured listings only, automatically accepted bookings only filters - the first and 3rd ones should exclude featured listings unless they’re also applicable.

Cheers,
Chris :victory_hand:

Hi Chris,

It may be easy for listing attributes of Checkbox type – you can try using the hivepress/v1/models/listing/attributes hook, and for the attribute (e.g. “booking_moderated”), add the “search_field” parameters and also the “filterable” flag set to true (next to “editable” Loom | Free Screen & Video Recording Software | Loom) The rest will be done automatically, the filter should appear in the form. The downside is that it follows the field logic, so the filter in this example would actually mean “show listings with non-instant bookings”, to change the logic you can use the “hivepress/v1/models/listing/search” action hook, which provides access to the WP_Query object for listings.

For vendor attributes (if you search listings), it’s more complex, because a JOIN query would be needed to filter by the related vendor fields, or alternatively, making the Verified checkbox hidden and synced with the vendor profile, then you can filter directly using the approach described above.

Hope this helps

1 Like

Thanks for the reply, @ihor!

I’ve passed it onto AI to help use this approach, and it’s given me a few new snippets to try out, so I’ll test these out and report back soon.

Cheers,
Chris :victory_hand:

Thanks for the reply, @ihor!

I’ve passed it onto AI to help use this approach, and it’s given me a few new snippets to try out, so I’ll test these out and report back soon.

Edit: sadly, the HivePress AI doesn’t seem to be able to put this all together, and its latest attempts keep giving me critical errors. Would you be able to share one working example that I can then try to update for the others?

Cheers,
Chris :victory_hand:

Hi,

Please use this code snippet as your base:

add_filter(
	'hivepress/v1/models/listing/attributes',
	function ( $attributes ) {

		if ( isset( $attributes['booking_moderated'] ) ) {

			$attributes['booking_moderated']['filterable'] = true;
			
			$attributes['booking_moderated']['search_field'] = array(
				'label'  => 'Your_label_here',
				'type'   => 'checkbox',
				'_order' => 70,
			);
		}
		
		return $attributes;
	},
	1000
);

When the ‘Manually accept new bookings’ option is enabled, this checkbox will filter and display only those listings.

However, if you need to reverse the logic (similar to the verified vendor approach mentioned above), further research will be required. But those hooks that Ihor mentioned can be helpful.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.