Jump to content

Querying pages with sub fields


kriegar
 Share

Recommended Posts

Hi guys,

Got a wee issue that hopefully someone here can clear up.

I have a page with a field called features, the field type is page and it has pages that belong to it. I want to be able to query against that to return all the pages in a list that have one or more of those features ticked.

Here is my code at the moment. What I would have thought is it would be something like featured.subfield = 1.

              
 $query = '';
                if(isset($location) && $location != ''){
                    $query .= ",location=".$location;
                }
                if(isset($minPrice) && $minPrice != ''){
                    $query .= ",askingprice2>".$minPrice;
                }

                if(isset($bedrooms) && $bedrooms != ''){
                    $query .= ",number_of_bedrooms>=".$bedrooms;

                }

                if(isset($maxPrice) && $maxPrice != ''){
                    $query .= ",askingprice2<".$maxPrice;
                }

                $query .= ",start=".$start.",limit=".$limit;
                //var_dump($query);
                $saleproperties = $pages->get(1167)->children($query);
                $pagination = $saleproperties->renderPager(array('listMarkup' => "<ul class='pagination'>{out}</ul>"));
                echo $pagination;
Edited by kriegar
  • Like 1
Link to comment
Share on other sites

Hi kriegar, and welcome!
 
If I understand your question (and please forgive me if I don't), the subfield selectors are something you want to add to your selector string ($query above)? Incidentally, $query looks like it should work from what I can see, and you obviously (from var_dump($query); above), have a good handle on debugging your code.
 
Have you seen http://processwire.com/api/selectors/#subfield ? It should be as easy as you expect (PW often is, and some times even easier than  that).

Also, take a look at https://processwire.com/api/fieldtypes/repeaters/ as that talks about the same sort of thing.

Link to comment
Share on other sites

Hi Dave,

Thanks for the reply.

I usually work with framework based php so I have a good grasp of the code and know how I would do this with raw sql however im still a bit fresh to some of the processwire stuff.

Anyhoo yeah I tried that with the subfield stuff so I did $query .= ",features.garden=1"; however that just spits out an error saying unknown subfield.

Here is a screenshot of the field as it looks from the admin panel. maybe I can specify the sub page id? or would I be able to specify that its the sub pages title that I am querying against if that makes sense.

Selection_012.png

Link to comment
Share on other sites

You're using subfields the wrong way. "features" holds a few pages, which on their own hold fields. E.g. "features.title=Garden" would find all properties, which has (at least one) page selected in features, that has a title of "Garden". Subfields are in this case all the fields of the linked pages. You should be aware that theoretically titles could not be unique, therefore if you can't be sure about that you could fallback to using the id instead if the title. If you query multi-pagefields directly, then it automatically queries for id's so "features.id=1015" is the same as "features=1015".

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