Dear,
I’m am trying to create a menu on the vendor view page which has 3 seperate pages behind them:
Everything is working correctly, except for the count, for some reason whenever i am trying to retrieve the count via a query inside the menu it will get an override by the actual post_query of that specific page, so for example:
- If i have 1 listing and 2 sold listings, they both get a 1 instead of 1 and sold 2, while on the listings page
- When i am on the listing page the listings gets a 2 and the sold gets 2
How would i be able to correctly retrieve the counts for the different queries?
<?php
namespace HivePress\Menus;
use HivePress\Models;
use HivePress\Helpers as hp;
// Exit if accessed directly.
defined('ABSPATH') || exit;
class Vendor_Profile_Menu extends Menu
{
public function __construct($args = [])
{
$vendor = hivepress()->request->get_context('vendor');
$active_item_count = 0;
$menu_items = [];
// Get the listing count
$listing_count = Models\Listing::query()
->filter([
'user' => $vendor->get_user__id(),
'status' => 'publish',
'sold' => false,
])
->get_count();
if ($listing_count > 0) {
$active_item_count++;
$menu_items['listings'] = [
'label' => __('Listings', 'xxxx'),
'url' => get_permalink($vendor->get_id()),
'_order' => 10,
'meta' => $listing_count,
];
}
// Get the sold listing count
$listing_sold_count = Models\Listing::query()
->filter([
'vendor' => $vendor->get_id(),
'sold' => true,
'status' => 'publish',
])
->get_count();
if ($listing_sold_count > 0) {
$active_item_count++;
$menu_items['sold_listings'] = [
'label' => __('Sold', 'xxxx'),
'url' => hivepress()->router->get_url('vendor_view_sold_page', ['slug' => $vendor->get_slug()]),
'_order' => 20,
'meta' => $listing_sold_count,
];
}
// Get vendor feedback
$vendor_feedback = Models\Feedback::query()
->filter([
'vendor' => $vendor->get_user__id(),
])
->order(['created_date' => 'desc'])
->get();
$feedback_count = is_countable($vendor_feedback) ? count($vendor_feedback) : 0;
if ($feedback_count > 0) {
$active_item_count++;
$positive_count = 0;
foreach ($vendor_feedback as $review) {
if ($review->get_score() >= 2) {
$positive_count++;
}
}
$positive_percentage = ($positive_count / $feedback_count) * 100;
$menu_items['feedback'] = [
'label' => __('Feedback', 'xxxx'),
'url' => hivepress()->router->get_url('vendor_view_feedback_page', ['slug' => $vendor->get_slug()]),
'_order' => 30,
'meta' => number_format($positive_percentage, 0) . '%',
];
}
if ($active_item_count >= 1) {
$args = hp\merge_arrays(
[
'items' => $menu_items,
],
$args
);
parent::__construct($args);
}
}
}