Jump to content


Photo

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

solved

  • Please log in to reply
7 replies to this topic

#1 ClintonSkakun

ClintonSkakun

    Jr. Member

  • Members
  • PipPip
  • 24 posts
  • 12

Posted 13 May 2012 - 11:27 AM

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, 21 May 2012 - 11:02 AM.
Marked as solved


#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 13 May 2012 - 12:41 PM

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

@somartist | modules created | support me, flattr my work flattr.com


#3 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 14 May 2012 - 12:10 PM

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) { ... }


#4 Soma

Soma

    Hero Member

  • Moderators
  • 3,186 posts
  • 1743

  • LocationSH, Switzerland

Posted 15 May 2012 - 05:12 AM

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

@somartist | modules created | support me, flattr my work flattr.com


#5 ClintonSkakun

ClintonSkakun

    Jr. Member

  • Members
  • PipPip
  • 24 posts
  • 12

Posted 18 May 2012 - 11:27 AM

Oh cool, thanks guys!

#6 joshuag

joshuag

    Sr. Member

  • Members
  • PipPipPipPip
  • 109 posts
  • 71

  • LocationCalgary AB, & Mérida, Yucatan

Posted 31 July 2012 - 02:15 PM

This is great! Thanks for asking this before me Clinton!

#7 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 788 posts
  • 205

  • LocationMidlands, UK

Posted 13 December 2012 - 07:54 AM

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


#8 ryan

ryan

    Hero Member

  • Administrators
  • 5,771 posts
  • 3108

  • LocationAtlanta, GA

Posted 17 December 2012 - 03:18 PM

Could this line be the problem? Notice you are checking statusHidden twice rather than statusUnpublished:

if ($galleries_page->is(Page::statusHidden) || $galleries_page->is(Page::statusHidden)) {






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users