Skip to content

Listing Query

Functions for interacting with the active DMS listing query and its filters.


Functions

dms_listing_query(): ?Query

Returns the Query singleton that manages the active listing query. Most use cases are covered by the helper functions below, but direct access is available when needed.

Returns - ?DMS\Listing\Query

Example

$query = dms_listing_query();

dms_get_total_listings(): int

Returns the total number of listings found by the current query (respects found_posts, not just the current page).

Returns - int

Example

$total = dms_get_total_listings();

echo sprintf( 'Showing %d listings', $total );

dms_get_listing_query()

Returns the underlying WP_Query object for the current listing query.

Returns - WP_Query

Example

$wp_query = dms_get_listing_query();

if ( $wp_query->have_posts() ) {
    while ( $wp_query->have_posts() ) {
        $wp_query->the_post();
        // ...
    }
}

dms_set_listing_query( array $args ): object

Overrides the current listing query with a new set of args. Returns the resulting WP_Query object.

Parameters

Name Type Default Description
$args array - WP_Query-compatible args

Returns - WP_Query

Example

$query = dms_set_listing_query( [
    'posts_per_page' => 6,
    'meta_key'       => 'dms_price',
    'orderby'        => 'meta_value_num',
    'order'          => 'ASC',
] );

dms_get_selected_terms(): array

Returns the category terms currently selected as active filters in the listing query.

Returns - array

Example

$selected = dms_get_selected_terms();

foreach ( $selected as $term ) {
    echo esc_html( $term->get( 'term' ) );
}

dms_add_listing_filter( Listing\Category $category, Listing\Category\Term $term ): void

Programmatically adds a category/term filter to the active listing query.

Parameters

Name Type Default Description
$category DMS\Listing\Category - The category to filter on
$term DMS\Listing\Category\Term - The term value to filter by

Returns - void

Example

$category = dms_get_category( 5 );
$term     = $category->has_term( 'Sedan', 'term' );

if ( $term ) {
    dms_add_listing_filter( $category, $term );
}