Jump to content
Vineet Sawant

Using $pages in custom function

Recommended Posts

Hello all,

So me being a noob can't figure out how can I use $pages->get or find in my custom function.

function printTitle($page_id){
     echo "Title of the page is ".$pages->get("id={$page_id}");

Above code is giving me error :

 Error: Call to a member function get() on a non-object 

From what I've read probably, I should be using $wire('pages')->get.

But this gives following error:

Error: Function name must be a string

Removing $ from wire solves above error but it doesn't print anything.

Any help would be very much appreciated.

Thanks a lot.

Share this post

Link to post
Share on other sites

Vineet - it's all about variable scope. $pages (or any of the other PW variables) can't exist inside a function like that.

You need to use: wire('pages')->get or define $pages = wire('pages') at the start of your function and then you can use: $pages.

Nevermind - sorry, apparently I didn't actually read your post :)

Looks like renobird's suggestion should be spot on!

Share this post

Link to post
Share on other sites


You are getting the page correctly, try adding title to the end.

echo wire('pages')->get("id={$page_id}")->title;

...and perhaps what onjegolders mentioned as well. I think omitted title should still output the page ID.

  • Like 1

Share this post

Link to post
Share on other sites

Thank you guys for quick reply, will try defining $pages so I have following code now. And it prints nothing :(

function printPage($page_id){
	$pages = wire('pages');
	echo "Page Title: ".$pages->get("id=$page_id")->title;
Edited by Vineet Sawant

Share this post

Link to post
Share on other sites

Are you sure $page_id is being passed as expected?

Perhaps echo $page_id to verify.

  • Like 2

Share this post

Link to post
Share on other sites

I'm having the same issue.

This is my function (which is sit on another php file )to print archive post, similar as the wordpress archive widget

function archive () {
$out = "
$start    = new DateTime('2010-01-01');
//$start->modify('first day of this month');
$end      = new DateTime('2015-02-01');
//$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

$a = array();
foreach ($period as $dt) {
    array_push($a, $dt);
$a = array_reverse($a);

foreach ($a as $dt) {
    $d = $dt->format("Y-m-d");
    $nextmonth = date('Y-m-d', strtotime('+1 month', strtotime($d)));
    $count = count($pages->find("template=post, post_date>=$d, post_date<$nextmonth"));
    if ($count > 0) {
        $out .= "<a href='{$pages->get('template=home')->url}" . date('Y', strtotime($d)) . "/" . date('m', strtotime($d)) . "'>" . date('M Y', strtotime($d)) . " (" . $count . ")" . "</a><br/>";

return $out;

In a template file, the code will looks



$out = "This is content.";

$sidebar = archive();

$page->body .= $out;

include('./main.php');  // the master output file

I will get this error in the log

Error:     Call to a member function find() on a non-object (line 26 of /var/www/html/pw1/site/templates/archive.php)

  • Like 1

Share this post

Link to post
Share on other sites

It's not only the same error in the log, but you're also doing the same thing wrong. You can't use $pages in functions because of variable scope. ProcessWire makes these object automatically available for the scope of the templatefiles, but inside a function is a new scope, so you need to either redefine the variable itself or use a function to get the object. 

// Template scope

$id = 15; // simple variable, defined by you
$pages = $pages; // the pages object, defined automatically by processwire

function something(){ 
  // this is now a new variable scope 
  // neither $pages nor $id are available here.
  // the api variables are not a special global variable

  $pages = wire('pages');
  // OR just

It's the same reason, why you can't use $pages in modules. $page/$pages and the other variables are just convenient to use in templates. Everywhere else you need to call them differently.

  • Like 7

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
      The error reported:
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.

      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Install the Custom Paths module.
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
    • By Spyros
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.


      Step 2: Select the images with the trashcan symbol

      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
  • Create New...