Jump to content

has() selector like JQuery


PHPSpert
 Share

Recommended Posts

It would be nice if ProcessWire had a has() selector like JQuery does. For example, so we could do this:

$pages->get("template=accounts")->has("created<=" . strtotime("-1 days"));

Is there something like that already or has it not been added?

Link to comment
Share on other sites

$pages->get("template=accounts, created<=-1 day");

Understood, but what if you want to get a page with children with a specific attribute?

Maybe more specifically:

$pages->get("template=accounts")->hasChild("created<=" . strtotime("-1 days"));

Link to comment
Share on other sites

$pages->find('created<=yesterday')->first()->parent()

or shorter:

$pages->get('created<=yesterday')->parent()

Edit: but these will return only the parent of one of the pages created in that time, not all of them.

  • Like 1
Link to comment
Share on other sites

I think that these guys posted the best solutions for what you are trying to do, since they cause the operation to take place before page is actually loaded. But if you do have a need for a comparison function such as that, you can use $page->is("selector") or $page->matches("selector"); which are similar to what you were looking for with a has() method. When used with a selector, both of the functions do the same thing. The only difference with is() is that you can also send it a template name, like $page->is("product"); or a status like $page->is(Page::statusHidden). Both functions return a boolean true or false. 

Link to comment
Share on other sites

$pages->find('created<=yesterday')->first()-> parent()

I seriously didn't know this was possible.

I guess if you're looking for a parent from multiple levels you could do

$pages->find('created<=yesterday')->parents()->get("template=top"); // This obj exists so that means the children exist...

NICE!

Link to comment
Share on other sites

You can also use the last get() selector inside the parents() and remove get() on the end. It new in 2.3 to use selector with parents in think and you can now also use closest(selector).

But using find() in your example and then parents() will not work because find always returns a page array and parents needs one page object. Instead of find you could use a get() like diogo showed.

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