Listings¶
Functions for retrieving and manipulating listing objects.
See the Listing object reference for all methods available on the returned object.
Functions¶
dms_get_listing( int $listing_id, bool $force_new = false ): Listing¶
Retrieves a single listing object by post ID. Results are statically cached per request by ID unless $force_new is true.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$listing_id |
int |
- | The listing post ID |
$force_new |
bool |
false |
Skip the static cache and instantiate a fresh object |
Returns - Listing
Example
$listing = dms_get_listing( 42 );
echo esc_html( $listing->get_title() );
echo esc_url( $listing->get_permalink() );
echo esc_html( $listing->get_formatted_price() );
dms_get_all_listings( array $args = [] ): array¶
Returns an array of Listing objects for all listings belonging to the current inventory type. Results are cached.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$args |
array |
[] |
Optional WP_Query-compatible args to filter results. Also accepts a type key (a Type object) to target a specific type, and a select bool to return [id => title] pairs instead of full Listing objects. |
Returns - Listing[] (array keyed by listing ID)
Example
// All listings for the current type
$listings = dms_get_all_listings();
foreach ( $listings as $id => $listing ) {
echo esc_html( $listing->get_title() );
}
// Listings with a custom WP_Query arg
$listings = dms_get_all_listings( [
'posts_per_page' => 10,
'meta_key' => 'dms_is_sold',
'meta_value' => '',
] );
// Simple select array [id => title]
$options = dms_get_all_listings( [ 'select' => true ] );
dms_get_current_listing(): ?Listing¶
Returns the listing currently active in the DMS loop context. Returns null if no listing is set.
Returns - ?Listing
Example
dms_get_current_listing_id(): ?int¶
Returns the ID of the current listing in context. Returns 0 if no listing is set.
Returns - ?int
Example
dms_set_current_listing( int|Listing $listing, bool $is_main_query = false ): void¶
Sets the active listing in the DMS loop context. Useful when manually iterating listings outside the main query.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$listing |
int\|Listing |
- | A listing ID or a Listing object |
$is_main_query |
bool |
false |
Whether this listing is part of the main query |
Returns - void
Example
foreach ( $listings as $listing ) {
dms_set_current_listing( $listing );
// Template functions that rely on the current listing context will now use this listing
get_template_part( 'partials/listing-card' );
}
dms_reset_current_listing(): void¶
Resets the current listing context back to the main query listing.
Returns - void
Example