Render listings in the WP_Query loop

Good day. This plugin is good, but not developer–friendly. Examples and different usage cases are missing in some docs.

Anywho, I’m trying to create a raw custom WP Query to list certain listings that meet a certain condition, then render the listings using the default display, However. can’t seem to find a function that does this. Have tried to read the docs with no success.

add_shortcode( 'hp_price_range_query', function( $atts ) {
    // 1. Setup attributes
    $atts = shortcode_atts( [
        'min'    => 0,
        'max'    => 999999,
        'number' => 4,
    ], $atts );

    // 2. Configure WP_Query
    $args = [
        'post_type'      => 'hp_listing',
        'post_status'    => 'publish',
        'posts_per_page' => (int) $atts['number'],
        'meta_query'     => [
            'relation' => 'AND',
            [
                'key'     => 'hp_price', // Standard HivePress price key
                'value'   => [ (float) $atts['min'], (float) $atts['max'] ],
                'type'    => 'NUMERIC',
                'compare' => 'BETWEEN',
            ],
        ],
    ];

    $query = new WP_Query( $args );
    $output = '';

    // 3. Loop through results
    if ( $query->have_posts() ) {
        $output .= '<div class="hp-listings hp-grid">';
        
        while ( $query->have_posts() ) {
            $query->the_post();
            
           //this is where to render the listings
               
        }
        
        $output .= '</div>';
        wp_reset_postdata();
    } else {
        $output = '<p>No listings found in this price range.</p>';
    }

    return $output;
} );

I’m using the core WP queries as they’re being used by hivepress under the hood, and more understandable.

I want some function that I can place in the loop and pass the post (listing) to

Thanks for your message — our team will reply shortly.

In the meantime, you can also get instant help from our AI Assistant, familiar with all the docs and solutions we’ve shared over the years.

Hi,

You can try this approach:

$listing = \HivePress\Models\Listing::query()->get_by_id( get_post() );

if ( $listing ) {
	$output .= ( new \HivePress\Blocks\Template(
		[
			'template' => 'listing_view_block',

			'context'  => [
				'listing' => $listing,
			],
		]
	) )->render();
}

This code snippet gets the HivePress listing object based on the WP_Post object in the loop, and then renders the listing template HTML, passing the listing object to its context.

We also plan to make the price filter (and other filters) available in the Listings block (and shortcode) in future updates. Currently only drop-down filters are available if you mark them as Filterable or Searchable.

Hope this helps

Helped a lot. This is what I’ve been looking for. Where’s the full doc of this? What different configurations can I put inside that array, like setting columns etc. I don’t want to recreate my own listing style for consistency

There’s no 100% coverage in dev docs yet, but you can check them here Getting started | Developer Docs Also you can check hook and code references, it’s possible to customize almost everything via hooks (actions and filters). I also recommend using our AI Assistant, since there were thousands of code snippets suggested on the forum it may suggest pretty specific solutions.

For the purpose you described, I recommend another approach – hooking into the query via the pre_get_posts hook (for example), this way you don’t have to implement the Listings block/shortcode from scratch just to add the price filter. If you decide to implement it anyway, you can add columns as HTML wrappers, please check how these are added here hivepress/includes/blocks/class-listings.php at master · hivepress/hivepress · GitHub

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