cms icon indicating copy to clipboard operation
cms copied to clipboard

entries_count includes published and unpublished entries

Open stephenmeehanuk opened this issue 4 years ago • 15 comments

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):

  • Fresh install from statamic/statamic

stephenmeehanuk avatar Apr 16 '21 13:04 stephenmeehanuk

Related to #2721.

duncanmcclean avatar Apr 16 '21 13:04 duncanmcclean

@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 }}

mikemartin avatar Jun 01 '21 04:06 mikemartin

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

jasonvarga avatar Jun 01 '21 13:06 jasonvarga

Is there any progress on this?

dominikfoeger avatar Nov 04 '22 09:11 dominikfoeger

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

binoclard avatar Jun 20 '23 14:06 binoclard

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?

j-jalving avatar Jul 05 '23 16:07 j-jalving

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

tenmillionteeth avatar Jul 09 '23 08:07 tenmillionteeth

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.

SG-fabian avatar Oct 05 '23 08:10 SG-fabian

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

KevinGrant12 avatar Jun 25 '24 22:06 KevinGrant12

{{ 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()

mehdismekouar avatar Aug 28 '24 11:08 mehdismekouar

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.

duncanmcclean avatar Aug 28 '24 13:08 duncanmcclean

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 avatar Aug 28 '24 14:08 mehdismekouar

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

mehdismekouar avatar Aug 28 '24 14:08 mehdismekouar

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

duncanmcclean avatar Aug 28 '24 17:08 duncanmcclean

Much respect sir! Thank you for your time

mehdismekouar avatar Aug 28 '24 19:08 mehdismekouar