Get count of listings with different meta field

Dear,

I’m am trying to create a menu on the vendor view page which has 3 seperate pages behind them:

image

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);
        }

    }

}

Please try to var_dump both the $listing_count and $listing_sold_count, do they differ in value? I also noticed that queries are different, e.g. in the first query you filter listing by user, and in the second one by vendor, I recommend filtering both by user 'user' => $vendor->get_user__id(), because in some cases admin can have multiple linked vendor profiles, maybe this is the reason for incorrect calculations.

Hope this helps

Hey Ihor,

Thank you for you reply, unfortunately that was not the fix, but it is more consistent so changed it anyway, after adding some dumps i get this:

image

But it should be default listing 7 and sold 16, when i go to the sold page it’s both on 16, so it’s overwriting the query with the one i have to render the page, like this for sold page:

Any idea how i can bypass this?

Please check if it’s something related to the “sold” filter, maybe there’s a bug related to checkbox filters (make sure that the attribute name is “sold” and it has a checkbox type), you can test other filters for a listing (attribute-based) to check if the counts become different. Also, you can try replacing “true” with “1” to check if the checkbox filter starts working. If the bug is with the filter we’ll fix this in the next update, but we need to reproduce the issue.

Hi Ihor,

The filter works as expected as i get the right posts on the default listings and sold listings page, just the counts in the menu are set to the count of the current post_query context instead of the queries inside the menu to retrieve the count for each.

I recommend using request context to pass values without being affected by queries. If these counts are available before the query, please try something like this:

hivepress()->request->set_context('sold_listing_count', 123);

Then in the template part, you can:

echo esc_html( hivepress()->request->get_context('sold_listing_count') );

Thank you for your reply, but the query was never returning the right count for both as the main post_query was overwriting it. I’ve decided to just add them to the vendor model and update them when a listing update action happens, which fixed the issue aa i could get them from the post meta. Thanks!