Jump to content
anowitz

using wire("pages")->find with "|" operator and multiple selectors

Recommended Posts

I'm building my first site with PW and am absolutely loving it.  I've spent a lot of time reading the forums--fantastic community!--but can't seem to find the answer to my question....so here goes.

I'm building a website for an art gallery and would like to have a section on the "exhibitions" page called "related news and press." 

The page hierarchy looks something like this

"exhibitions-parent" (not visible)

--"picasso exhibit"

"news-parent" (not visible)

--"Picasso exhibit opens!"

--"New book about Picasso's life and times released"

"press-parent" (not visible)

--"Review of Picasso exhibit in the NYTimes"

Children of "news-parent" use the template "news," which contains two relational page fields called "news_artist" and "news_exhibition"

Children of "press-parent" use the tamplate "perss," which contains two relational page fields called "press_artist" and "press_exhibition"

In my "related news and press" section I'd like to output all news or press items that are linked to either the exhibit or the artist.

I know that the "|" can be used in selectors like this: firstname=Mike|Steve

What I'd like to do, however, is something like this:

$recent_news_and_press = wire("pages")->find("press_exhibition=$exhibition_id | news_exhibition=$exhibition_id | press_artist=$exhibition_artist | news_artist=$exhibition_artist");  

I know this is the wrong syntax, as it doesn't work.  Is there a way to do this in one find query, or do I need to run all those queries separately, and then combine them into an array?

Also, is there a risk of duplicate results when doing this sort of query, and if so does anybody have any advice on that?

Thanks so much!

Share this post


Link to post
Share on other sites

You can do:

press_exhibition=$exhibition_id, news_exhibition=$exhibition_id, press_artist=$exhibition_artist, news_artist=$exhibition_artist

Are you looking to replicate "and" or "or" ?

Share this post


Link to post
Share on other sites

Thanks for the quick reply Adrian.  I'm trying to replicate "or."  If I understand selectors correctly, doing "press_exhibition=$exhibition_id, news_exhibition=$exhibition_id, press_artist=$exhibition_artist, news_artist=$exhibition_artist" would replicate "and" - is that right?  Is there a way to replicate "or"?
 

Share this post


Link to post
Share on other sites

Actually haven't used this myself before, but try:

press_exhibition|news_exhibition|press_artist|news_artist=$exhibition_id|$exhibition_id|$exhibition_artist|$exhibition_artist

This may not work exactly as you want because it will check if any of the variables are in any of the fields, but depending on your structure and possible options for these fields it might be ok. If not, I think you might have to do multiple selections and combine them like you suggested.

Here is an example of combining:

http://processwire.com/talk/topic/3055-replicate-mysql-union-with-selectors/?p=30033

In these cases I have actually gone with using standard SQL:

http://processwire.com/talk/topic/3053-possible-to-select-modifiedcreated/?p=30093

Share this post


Link to post
Share on other sites

Looks like that worked - at least with the limited number of pages I've got in the dev site right now.  Thanks so much!

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

  • Similar Content

    • By fruid
      I'm having and always have a hard time building PaginatedArrays, I never know where to put the "limit=24" selector so please enlighten me.
      Here's what I'm doing…
      $categories = $page->protable('start=0, limit=999999'); // this I need in order to retrieve the pages's "categories" and I don't know how to make use of $rows instead for that purpose because of this confusing limit-API $rows = $page->protable; // put ("limit=20") here? $custom = buildSelector($input, $rows); // this function returns an array of selectors depending on the user input $items = new PaginatedArray; // or here? // or here? $items->find("limit=20") // or like this? $items("limit=20") // or like this? $items = $items("limit=20") // or like this? $items = $items->find("limit=20") foreach ($rows as $r) : if ($custom->get('selector')->matches($r)) : $items->add($r); endif; endforeach; // or at this point? and then
      if ($items) { // or maybe somewhere here?     echo '<span class="grey">'.$items->getPaginationString(array(     'label' => 'entries',     'zeroLabel' => '0 entries', // 3.0.127+ only     'usePageNum' => false,     'count' => count($items),     'start' => $items->getStart(),     'limit' => count($items),     'total' => $items->getTotal()     )); and then of course…
      $pager = $modules->get("MarkupPagerNav"); echo '<div class="uk-flex uk-flex-center">'.$pager->render($items, $options).'</div>'; I'm out of ideas and confused cause it doesn't make sense to me either way.
      ____
      The buildSelector function above returns and array…
      $selected = new Selectors("$letter, $searchterm, $category"); $custom = new Wirearray; $custom->set('selector', $selected); $built->set('sort', $sort); return $custom; and each of the new selectors are basically strings ("category=whatever")
      Also, you cannot put the "sort=title" or whatever as a selector for the Selectors function (see above). Why, I know not.
      I don't know if that is the proper way but selecting kind of works now as opposed to many other ways I tried. Selectors always require a lot of trial and error, it seems to have a very sensitive API, always depends on double quotes, single quotes and how you concatenate.
      Thanks for help!
    • By SwimToWin
      I want to add a dependent SELECT field on my template page that lists pages from a parent "sub-page" in the current parent node.
      On /product1/page I have the field "photo" which is a SELECT field.
      I want the SELECT to list pages from /ROOTPARENT/photos.
      The idea is that I can reuse the same photo in many places - but only need to keep it update it once under /product1/photos.
      My page structure looks like so:
      /product1/page /product1/photos/photo3 (template=photos) /product2/photos/photo9 I have tried adding these Selector Strings on the Field (Setup -> Fields -> PHOTO -> Input tab -> Selectable Pages field group -> Selector String):
      parent=/product1/page, template=photos, sort=name WORKS (but only on children of current product). parent=page.rootParent ... parent=$page.rootParent ... parent=$page.rootParent parent=$parent ... parent=$parent1 When using a SELECT Input Field Type, the editing pages gives the fatal error "Unrecognized operator: $". parent=parent ... parent=. Returns an empty list How might I find child pages from the current "/product1/photos/ page"?
      Your inputs are appreciated. Thanks.
    • By celfred
      Hello,
      I've just upgraded to 3.0.165 (and updated my Ubuntu version as well) and on my localhost, I am facing a weird issue : all my requests having the ~= selector cause a Mysql error with this message :

      PDOException #HY000 SQLSTATE[HY000]: General error: 3685 Illegal argument to a regular expression. I have no idea what is going on. If I change my request from

      $visualizer = $pages->get("name~=visualizer"); // Triggers the error
      to
      $visualizer = $pages->get("name=visualizer");
      or
      $visualizer = $pages->get("name~*=visualizer");
      My code works fine again.
      Any idea ? Shall I change all my requests (but from what I understand by reading the documentation, ~= exists and fits my needs : all words in any order [though I do understand that in my example above it may be useless since I have only one word])
      Thanks !
    • By Anders
      I want to allow full text search on my site. There is a very nice solution that comes right out of the box:
      $selector = "title|body~=$q, limit=50"; This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?
      I can see three possible paths, but I am not very fond of any of them:
      Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body? Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!
    • By iipa
      Hi!
      I have two Page Reference fields: Category and Subcategory. Category is parent of Subcategory, and can have 0 to n Subcategories. Fields are selected by dropdown selection.
      I'm trying to achieve following logic:
      1. Select Category (obviously works)
      2. Change Subcategory options based on selected Category (this works)
      3. Hide the Subcategory field if Category has no Subcategories (to prevent weird dropdown with nothing to select)
      I have tried to investigate two possible alternatives to achieve step 3:
      A) Make dynamic condition into Subcategory selection's "Only visible if..." field
      B) Make a hook that fires when Category changes, then hides Subcategory field if Category has no children
      So far I haven't been successful in either. Conditions I've tried always lead to hidden field, and I haven't been able to find set up a hook that fires on field change.
      So here's the question (finally): Is there a way to alter field visibility in API, for example in ready.php or inside a hook? In API I could loop through categories, find the ones that have children and then make a selector based on their IDs.
×
×
  • Create New...