Jump to content

How to set (un)Published for a page in the API


PHPSpert
 Share

Recommended Posts

I'm building a website that creates pages, sets the template and once a payment has been processed the page needs to be set to published.

I need to know how to set this in the PHP Page class.

Is it just $page = new Page(); $page->published = FALSE;?

I couldn't find out how to do this in the docs.

Thanks

Clint

Edited by adamkiss
Marked as solved
  • Like 2
Link to comment
Share on other sites

It's one of the statuses you see in the top of the Pages class.

So you can set the status using it like:

$page->status = Page::statusUnpublished;

Edit: Have you looked on the Cheatsheet? For reference under "Page Status" http://processwire.com/api/cheatsheet/

BTW aren't you the guy with the processwiresexy, whatever? I'm kinda surprised you didn't know this. :D

  • Like 4
Link to comment
Share on other sites

The page statuses are a bitmask so it's possible for there to be multiple statuses on a page, like unpublished and hidden. So it's best to add and remove statuses with the addStatus and removeStatus functions, i.e.

$page->addStatus(Page::statusUnpublished); 
// same as:
$page->status = $page->status | Page::statusUnpublished; 

$page->removeStatus(Page::statusHidden); 
// same as:
$page->status = $page->status & ~Page::statusHidden;

You can also check the status of a page by using the is() function:

if($page->is(Page::statusUnpublished)) { ... }
// same as
if($page->status & Page::statusUnpublished) { ... }
  • Like 13
Link to comment
Share on other sites

The page statuses are a bitmask so it's possible for there to be multiple statuses on a page, like unpublished and hidden. So it's best to add and remove statuses with the addStatus and removeStatus functions, i.e.

$page->addStatus(Page::statusUnpublished);
// same as:
$page->status = $page->status | Page::statusUnpublished;

$page->removeStatus(Page::statusHidden);
// same as:
$page->status = $page->status & ~Page::statusHidden;

You can also check the status of a page by using the is() function:

if($page->is(Page::statusUnpublished)) { ... }
// same as
if($page->status & Page::statusUnpublished) { ... }

Ups, almost forgot about them. *hidesinthecorner* :D

  • Like 2
Link to comment
Share on other sites

  • 2 months later...
  • 4 months later...

I'm having a problem where I can test for Hidden but the unpublished test is failing:

<?php
$events_page = $pages->get("template=events");
$galleries_page = $pages->get("template=galleries");
if ($events_page->is(Page::statusHidden) || $events_page->is(Page::statusUnpublished)) {
} else {
$events = $pages->find("template=event, sort=-event_date, limit=3");
}
if ($galleries_page->is(Page::statusHidden) || $galleries_page->is(Page::statusHidden)) {
} else {
$galleries = $pages->find("template=gallery, images.count>0, sort=-created, limit=3");
}

if (count($events) || count($galleries)) { ?>

Not too sure what's happening but have definitely set my Events page to unpublished...

I've got around it using:

<?php 
$events_page = $pages->get("template=events");
$galleries_page = $pages->get("template=galleries");
if ($events_page->viewable() && $events_page->isHidden() == false) {
$events = $pages->find("template=event, sort=-event_date, limit=3");
}
if ($galleries_page->viewable() && $galleries_page->isHidden() == false) {
$galleries = $pages->find("template=gallery, images.count>0, sort=-created, limit=3");
}

if (count($events) || count($galleries)) { ?>
Link to comment
Share on other sites

  • 1 year later...

In regards to this post, is there a way to give a role called frontend_editor a way to unpublish, republish and delete while keeping the page hidden from lists and searches as I have it now in the admin. I have certain pages assigned to users with the frontend_editor which they have access to edit. Any guidance?

Link to comment
Share on other sites

  • 1 year later...

The page statuses are a bitmask so it's possible for there to be multiple statuses on a page, like unpublished and hidden. So it's best to add and remove statuses with the addStatus and removeStatus functions, i.e.

$page->addStatus(Page::statusUnpublished); 
// same as:
$page->status = $page->status | Page::statusUnpublished; 

$page->removeStatus(Page::statusHidden); 
// same as:
$page->status = $page->status & ~Page::statusHidden;
You can also check the status of a page by using the is() function:
if($page->is(Page::statusUnpublished)) { ... }
// same as
if($page->status & Page::statusUnpublished) { ... }

It also looks like the published column in the pages table gets filled in with an indexed timestamp when it is published - so that allows a query to exclude unpublished pages efficiently (by selecting "and published != null". But hidden pages have to be retrieved and checked for hidden.

In theory the DB could use the index values but apparently mysql doesn't: https://stackoverflow.com/questions/5352263/optimize-mysql-query-to-use-index-on-a-bitwise-where-clause.

Edited by bmacnaughton
Fixed quote
Link to comment
Share on other sites

It also looks like the published column in the pages table gets filled in with an indexed timestamp when it is published - so that allows a query to exclude unpublished pages efficiently (by selecting "and published != null". But hidden pages have to be retrieved and checked for hidden.

No, don't do that. Previously published and then un-published pages still retain their timestamp. Always use the appropriate status flags / selector options.

Link to comment
Share on other sites

No, don't do that. Previously published and then un-published pages still retain their timestamp. Always use the appropriate status flags / selector options.

Thanks for the additional information.

So you're saying that if I have 1 million pages that PW must go through them all one-by-one (as a result of mysql not using the index value in bitwise operations) in order to find which pages are published?

Link to comment
Share on other sites

No you use

$pages->find("template=basic-page, status=unpublished");

Respectively 

$pages->find("template=basic-page, status=published");

But since 

$pages->find("template=basic-page");

returns only published pages anyway that are not hidden and above, a status=published makes no sense.

  • Like 3
Link to comment
Share on other sites

Thanks - that makes sense to me.

I was trying to understand BitPoet's comment though - if I interpret that correctly it's saying that PW itself would have to actually go through and look at the status of each page. That doesn't make sense to me - published must be an indexed column.

My original post was pointing out that there is a published column in the mysql "pages" table and that it looked like that could be used to efficiently filter out unpublished pages (or to find them) but that the same was not true for hidden.

Is my understanding correct?

Link to comment
Share on other sites

No, PW doesn't have to go through all pages, it's DB query. Field "published" is brand new and for timestamp of the publish date. If a page is published or not is in status and it's a bitmask.

  • Like 1
Link to comment
Share on other sites

MySQL (InnoDB) only very recently got the ability to allow index searches with bitwise operations. With the current (5.7.8) release, you can add a generated or virtual column to the pages table and index that. If the column operation in the query matches the column definition, the index is used. This should look something like the following (untested):

ALTER TABLE pages ADD stpublished INT AS (status & 2048) STORED;
CREATE INDEX idxstpublished ON pages (stpublished);

After that, any queries with matching conditions like "pages.status & 2048 = 2048" or "pages.status & 2048 > 0" should hit the index.

That's still a bit bleeding edge, but since Oracle officially promotes this feature (it's a must-have for the also advertised JSON column type to be of use), it should be reasonably stable by now. Of course, you have to have a generated column for every comparison you want to speed up, and having one adds another little performance penalty on insert operations.

  • Like 3
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...