Jump to content

Tags- exclude unpublished


Peter Knight
 Share

Recommended Posts

I have a list of Tags on a blog. They basically list all the tags used across all posts using the selector below.

<?php 
echo "<ul class=\"rhs-meta\">";
        
$browse_tags = $pages->find("template=el-tag-individual");
    
foreach ($browse_tags as $tag) {
echo"<li class=\"\"><a href=\"{$tag->url}\">{$tag->title}</a></li>";}

echo"</ul>"
;?>

I notice when a page is unpublished, tags used by a page are still included on the list of tags.

PW is just doing what I ask it- pull in all tags from pages with the template el-tag-individual

is there a way to exclude tags from unpublished pages?

The selector documentation refers to the opposite (including unpublished pages).

Link to comment
Share on other sites

Problem is, even the include unpublished is only testing the pages requested, means if the tag is unpublished or not. You can't get only tags used by published pages with a find selector.

$posts = $pages->find("template=posts"); // no unpublished ones
$tags = new PageArray();

foreach($posts as $post){
  $tags->import($post->tags);
}

$tags = $tags->unique();

Note that this does not scale well with growing number of posts. The more scalable way would be hooking Pages::save and Pages::delete and unpublish tags if no other visible page is using it anymore / republish them if they are used again.

Edit: And again missed the other variant

$tags = $pages->find("tags");

foreach($tags as $tag){
  if($pages->count("template=post, tags=$tag")) continue;

  // Found at least one possible post
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...