statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.9k stars 520 forks source link

entries_count includes published and unpublished entries #3539

Closed stephenmeehanuk closed 3 weeks ago

stephenmeehanuk commented 3 years ago

Bug Description

entries_count includes published and unpublished entries.

I'd like to see a way to filter this, in a similar way we can with {{entries status:is="published"}}

How to Reproduce

Add a term to three entries, mark one entry as a draft. entries_count will display the number 3.

<ul>
      {{ taxonomy:group }}
          <li>
              <a href="{{ url }}">{{ title }}</a> ({{ entries_count }})
          </li>
      {{ /taxonomy:group }}
</ul>

Environment

Statamic version: 3.1.5 Pro

PHP version: 7.4.11

Install method (choose one):

duncanmcclean commented 3 years ago

Related to #2721.

mikemartin commented 3 years ago

@arthurperton I have an entries fieldtype with published and unpublished entries, and augmentation removes the unpublished entries. There seems to be no possible way to include them at the moment.

Screen Shot 2021-06-01 at 2 31 02 pm

Would love to output them like Jason's solution. For example:

{{ features status:in="draft|published" }}
...
{{ /features }}
jasonvarga commented 3 years ago

@mikemartin looks like you've commented on the wrong issue?

dominikfoeger commented 1 year ago

Is there any progress on this?

binoclard commented 1 year ago

Hi, Is there any new updates or workaround concerning this issue?

I am using

{{ taxonomy:categories collection="projets|imagerie" }}
  <option value="{{ url }}">{{ title }}</option>
{{ /taxonomy:categories }}

And it outputs Terms that are assigned only to draft/unpublished entries.

{{ taxonomy:categories collection="projets|imagerie" status:is="published" }} does not work either.

Thanks

j-jalving commented 1 year ago

entries_count als includes future posts. I don't think unpublished or future should be included anywhere, only when explicitly requesting it.

Anyone found a workaround?

tenmillionteeth commented 1 year ago

A workaround I'm using is to check if the entry count is 0 (which only includes published entries), and if so, hide the taxonomy term from the listing.

For example:

{{ if {{collection:count from="collection" taxonomy:topics:any="{{slug}}"}} == 0}}

         {{else}}
          {{ title }} ({{ collection:count from="{{collection}}" taxonomy:topics:any="{{slug}}"}})
 {{/if}}
SG-fabian commented 1 year ago

This is still an issue. Our client unpublished some news and which let to empty news category pages. This solution is not unambiguous, since the taxonomy could be be unpublished:

{{ taxonomy:categories min_count="1" status:is="published" }}

But it would be really nice to have some kind of "clean" solution in the future.

KevinGrant12 commented 3 months ago

I am running into this same issue -- taxonomy is showing entries with unpublished content.

{{ taxonomy:categories collection:is="blog" }}
                        <a href="#" id="slug" data-category="category-{{ slug }}" class="sidebar-nav-item {{ if active }}active{{/if}}">{{ title }}</a>
                    {{ /taxonomy:categories }}
mehdismekouar commented 1 month ago
{{ taxonomy:tags collection="articles" min_count="1" status:is="published" }}

The above code throws an error

Call to undefined method Statamic\Stache\Query\TermQueryBuilder::whereStatus()
duncanmcclean commented 1 month ago

Terms don't really have statuses, in the same way that entries do.

The {{ entries_count }} won't be affected by any parameters on the taxonomy tag anyway, since that's not handled by the query.

mehdismekouar commented 1 month ago

Thanks for the clarification

But i tried {{ {taxonomy:tags collection="articles" min_count="1" title:contains="test"} | count }} and it does affect the count, i tried other filters and the count gets affected too, only the "published" filter doesn't

Any help on that?

mehdismekouar commented 1 month ago

Oops! i think it's my bad

The title in title:contains="test" refers to the tag title and not to the articles entries title I get it now, sorry for that

Now my real question should be, how can i query the taxonomy:tags of collection="articles" where the articles entries are published only?

Any guidance on that? Thanks for your patience

duncanmcclean commented 1 month ago

For now, you can workaround it with a custom tag:

// app/Tags/TermEntriesCount.php

<?php

namespace App\Tags;

use Statamic\Facades\Entry;
use Statamic\Tags\Tags;

class TermEntriesCount extends Tags
{
    /**
     * The {{ term_entries_count }} tag.
     *
     * @return string|array
     */
    public function index()
    {
        return Entry::query()
            ->where('collection', $this->params->get('collection'))
            ->whereTaxonomy($this->params->get('term'))
            ->whereStatus('published')
            ->count();
    }
}
{{ taxonomy:tags }}
    <a href="{{ url }}">{{ title }} ( {{ term_entries_count collection="articles" :term="id" }} )</a>
{{ /taxonomy:tags }}
mehdismekouar commented 1 month ago

Much respect sir! Thank you for your time