Is there any code to get the ID, slug or category name of the current listing?

I was testing and found a way to get the name, but it is not very efficient because it has to go through a loop. Is there another more efficient way?
I can use any id, slug or name.

if ( is_singular( 'hp_listing' ) ) {
    $listing = \HivePress\Models\Listing::query()->get_by_id( get_queried_object_id() );
    if ( $listing && $listing->get_categories__id() ) {
        foreach ( $listing->get_categories() as $category ) {
            $category_name = $category->get_name();
        }
    }
}

Yes, this is the best way to do this, you can also try getting an array of fields this way:

$listing->get_categories__name()

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