Jump to content

[SOLVED] Using $pages->find in a module's function


formmailer
 Share

Recommended Posts

Hi!

I am probably thinking totally wrong, but I would like to run the following $pages->find query directly in a module's function  

$pages->find("status=unpublished");

I don't want to run it in the template and pass the PageArray to this function, but when I am trying this I get the following error:

Call to a member function find() on a non-object

Is it possible to do this in some way or does it require a totally different approach?

/Jasper

Link to comment
Share on other sites

This should work everywhere:

wire("pages")->find("status=unpublished")

Also you can view all unpublished pages through search page also:

/processwire/page/search/?q=unpublished&show_options=1&field=status&template=&submit=Submit+Search&operator=%3D&display=&sort=relevance
Link to comment
Share on other sites

Hmmm... I need to make the find query more complex and I am not sure how I can fix this. I have:

wire("pages")->find("status=unpublished, publish_from<" . time() .", publish_until>" . time());

The above works, but I also need the the pages without a publish_until date (publish_until = empty).

How can I do that ? Is it possible?

/Jasper

Link to comment
Share on other sites

It sounds like you want publish_until to serve two purposes: as a toggle and as a date. I don't think you can have it do both, but here's a couple alternate approaches:

1. Leave the publish_until out of the selector and check it in your loop afterwards:

<?php
$time = time(); 
$matches = wire('pages')->find("status=unpublished, publish_from<$time");
foreach($matches as $m) if($m->publish_until > 0 && $m->publish_until < $time) $matches->remove($m); 

2. Or; use two find()s:

<?php
$time = time(); 
$matches = wire('pages')->find("status=unpublished, publish_from<$time, publish_until>$time");
$matches->import(wire('pages')->find("status=unpublished, publish_from<$time, publish_until=0"); 
Link to comment
Share on other sites

Thanks Ryan! As you probably understood I am writing a module for scheduled posts and I thought everyting worked, but suddenly I realised that you easily can have pages with a start date but without an end date, so I need to take care of these as well.

Just ignoring the end date actually works, but causes expired pages to be activated and be deactivated directly afterwards by the "unpublish rule".

I'll try your options out and I am sure they will work.

/Jasper

Link to comment
Share on other sites

  • 3 months later...

Thanks Ryan! As you probably understood I am writing a module for scheduled posts and I thought everyting worked, but suddenly I realised that you easily can have pages with a start date but without an end date, so I need to take care of these as well.

Just ignoring the end date actually works, but causes expired pages to be activated and be deactivated directly afterwards by the "unpublish rule".

I'll try your options out and I am sure they will work.

/Jasper

(embarassed to ask... but the shame isn't too deep!)

Module completed and available to public? (whistling as if no one heard a thing...) ;P

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