Hello
I have a solution to the problem of displaying and searching for listings by id number.
This is based on earlier “Unique number or ID” posts.
First you need to add the attribute to listings. In my case it’s “numer_ogloszenia” (In WP - Listings->Attributes->Add new)
The attribute should not be editable by the user.
Text.
It should be searched by keywords.
It can be added to a block or page.
In addition, you can use the shortcode [numer_ogloszenia_hp]
When a listing is created, its id is written to this attribute (numer_ogloszenia), and the attribute can be searched for and displayed.
// this displays id under listing title
add_filter(
'hivepress/v1/templates/listing_view_page/blocks',
function ($blocks, $template){
$listing = $template->get_context('listing');
if(!$listing){
return $blocks;
}
return hivepress()->helper->merge_trees(
[ 'blocks' => $blocks ],
[
'blocks' => [
'page_content' => [
'blocks' => [
'listing_unique_id' => [
'type' => 'content',
'content' => '<font size="2">Numer ogłoszenia #'.$listing->get_id().'</font>',
'_order' => 19,
],
],
],
],
]
)['blocks'];
},
1000,
2
);
// shortcode: [numer_ogloszenia_hp]
add_shortcode(‘numer_ogloszenia_hp’, ‘id_numer_ogloszenia’);
function id_numer_ogloszenia( $listing_id, $value ) {
$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
if(!$listing){
return;
}
$numer_ogloszenia1 = $listing->get_id();
return $numer_ogloszenia1;
}
// first create listings → attribute: numer_ogloszenia
// none editable by user
// text
// enable searching by keywords
// can be displayed in block or page
add_action(
'hivepress/v1/models/listing/create',
function ($listing_id, $listing){
$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
if(!$listing){
return;
}
$listing->set_numer_ogloszenia($listing->get_id())->save_numer_ogloszenia();
},
1000,
2
);