Join our new community forum for support & discussion

Join Now

Home Support General HivePress Programmatically render listing from listing post id

Programmatically render listing from listing post id

  • Author
    Posts
  • frshminds

    i have a set of query results generated using WP_Query and i want to render the listing markup for each found listing post.

    frshminds

    quick clarification.. looking to render the listings as they are on the archive pages.

    yevhen developer

    Please try this code snippet

    echo ( new HivePress\Blocks\Template(
    	[
    		'template' => 'listing_view_block',
    
    		'context'  => [
    			'listings'         => new HivePress\Models\Listing::query()->get_by_id( get_the_ID() ),
    		],
    	];
    ) )->render();
    frshminds

    i get the error:

    PHP Parse error: syntax error, unexpected ‘query’ (T_STRING), expecting variable (T_VARIABLE) or ‘$’

    yevhen developer

    Please try this code snippet instead

    // query posts here
    echo ( new HivePress\Blocks\Template(
    	[
    		'template' => 'listing_view_block',
    
    		'context'  => [
    			'listings'         => [],
    		],
    	];
    ) )->render();
    frshminds

    after removing the syntax error from the above code, it still does nothing.

    yevhen developer

    Please make sure that there is a global listing query before this code (via query_posts), if there are listings queried this code renders the listing grid. I also tested it locally and it seems to be ok.

    frshminds

    i do have a list of posts returned from a query. not all of the posts are hp listings. i need to render the listings. can i pass an id to render 1 listing?

    yevhen developer

    Unfortunately there is no easy way to render a regular post as a listing, the listing block expects the “hp_listing” post type (passed as a HivePress model object).

    frshminds

    i don’t want to render a regular post as a listing.
    i want to render a single listing block. i have the id.

    frshminds

    the query i’m working w/ is a search query generated by Search WP.
    i am rendering all the different post types as their native markup.
    for instance, for a Woocommerce product i can just go like this:

    $product = wc_get_product($pid);
    wc_get_template_part(‘content’, ‘product’);

    so if i have a listing id and i want to render the listing block (as it would be on a listing archive page or at the bottom of a single listing page), what would i do?

    ihor developer

    You can try this code snippet (replace 123 with the listing ID):

    echo ( new HivePress\Blocks\Template(
    	[
    		'template' => 'listing_view_block',
    
    		'context'  => [
    			'listing'         => HivePress\Models\Listing::query()->get_by_id( 123 ),
    		],
    	];
    ) )->render();

    We use it in the same way in the Listings block https://github.com/hivepress/hivepress/blob/master/includes/blocks/class-listings.php#L307

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

New Reply

This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.