Jump to content

Don't display page reference from unpublished pages


mjut
 Share

Recommended Posts

I am building a web site with a blog / posts section. With each post I create I can add tags. That is happening with a page reference field. For each new tag a new tag (page) will be created.

On an "archive" page, I want to list all tags with this code:

<ul>
<?php
     $tags = $pages->find("template=tag, sort=title");
     foreach ($tags as $tag) { ?>
     <li><a href="<?php echo $tag->url ?>"><?php echo $tag->title ?></a></li>
<?php	} ?>
</ul>

Simple as that, it works as expected. All tags used anywhere in my posts are listed. (yaaay!)

But:
When creating a new post without publishing, new tags be created and will listed on my archives pages.

How can I modify my code to exclude all tags that are associated with unpublished posts?

Cheers
Stephan
 

 

 

Link to comment
Share on other sites

Also you can use owner selector like

template=tag, tags.owner.template=post, limit=10

Where 'tags' is your page reference field used in blog post template and 'post' is the name of this template.

  • Like 1
Link to comment
Share on other sites

@Zeka Thank you tons!

Yet again, it amazes me how simple Processwire is. I went for the owner selector. Sorry, I did not know about this. The functional code looks like this:

<ul>
<?php
     $tags = $pages->find("template=tag, tags.owner.template=post, sort=title");
     foreach ($tags as $tag) { ?>
     <li><a href="<?php echo $tag->url ?>"><?php echo $tag->title ?></a></li>
<?php	} ?>
</ul>

As simple as that.

I looked it up in the docs, but did not find it – but there is a blog post about Owner Selectors: https://processwire.com/blog/posts/processwire-3.0.95-core-updates/

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...