Peter Knight

User - display field values from their most recent created page

Recommended Posts

On the road and typing on mobile. Hope this makes sense...

I have a loggedin user (current session)

I want to display to that user some values from the their most recently created page (singular)

Is this the right direction to be looking?
http://cheatsheet.processwire.com/users/users-methods/users-find-selector/

Thanks

 

 

Share this post


Link to post
Share on other sites
$mostRecentlyCreatedPageByUser = $pages->get("created_users_id=$user->id,sort=-created");

 

  • Like 2

Share this post


Link to post
Share on other sites
57 minutes ago, kixe said:

$mostRecentlyCreatedPageByUser = $pages->get("created_users_id=$user->id,sort=-created");

 

Thanks @kixe

Couple of questions for you

Do I need the sort in there? I'm only trying to retrieve values from the single last created page. 

I've tried the following which outputs nothing

<?php $mostRecentlyCreatedPageByUser = $pages->get("created_users_id=$user->id,sort=-created"); ?>

<!-- START: Information -->
<strong>City:</strong><?=$mostRecentlyCreatedPageByUser->city ?>
<br/>
<strong>Miles:</strong><?=$mostRecentlyCreatedPageByUser->miles ?>      
      
        
        

 

 

 

 

 

Share this post


Link to post
Share on other sites

@Peter Knight

Yes, you have to add 'sort = -created' otherwise you will get 'sort=id' by default. In contrast to find(), get() returns only a single page (the first page from the PageArray to which your selector applies). If you get nothing, the user has not yet created a page or the user is the guest user (not loggedin), or the page have the status hidden or unpublished. You can add selectors like include=hidden|unpublished|all.

Edited by kixe
see next post by kongondo
  • Like 1

Share this post


Link to post
Share on other sites

Thanks for the help @kixe and @kongondo

I got it working with a small change. 

It only works when add the name of the template to the selector.

<?php $mostRecentPage = $pages->get("created_users_id=$user->id, sort=-created, template=places");?>

Luckily in this project, my clients only create pages based on this template.

  • Like 1

Share this post


Link to post
Share on other sites

@Peter Knight

Good that it works. It is always best practice to choose the selector in a way that the mysql query is as accurate as possible and thus fast.
I'm still surprised that the original selector returns nothing. ???

Share this post


Link to post
Share on other sites
2 minutes ago, kixe said:

@Peter Knight

I'm still surprised that the original selector returns nothing. ???

Yep same here. Not sure if it's a bug or intentional. I might file it on GitHub

Share this post


Link to post
Share on other sites

Quick check

  • debug mode on?
  • any warnings, notices, errors?
  • does $mostRecentlyCreatedPageByUser->id return something > 0?
  • is $mostRecentlyCreatedPageByUser->city and $mostRecentlyCreatedPageByUser->miles populated?

You should check the latter 2 in your template before echo.

  • Like 1

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.