ZionBludd Posted April 23, 2015 Share Posted April 23, 2015 (edited) Hi All, forgive me for my question. I am building a reviews website, where people are able to submit listings and reviews of particular items. My website or "app" functions as below: HomepageSimple static page with a hero/search box, etc Search Results PageOne of the core pages, currently using a solution where WP_Query searches for results and returns them.Currently controlled by WP_Query search command, bringing back results. I've looked up how to do this PW, and the only way is via {foreach} - is this correct? Or is there a better way? Listing PageDisplaying a whole bunch of custom fields I've grown to dislike Wordpress simply because it feels sluggish and is too much for what I want to do - which is accept data from users, enter some data myself, and show that data. It's a very basic reviews website, nothing too fancy IMO. My main concerns about shifting from WP to PW is the search results page - Is there more control over the results than using foreach. Code i've managed to get working so far - but for instance, what If I need to sort the items by location, etc, etc? <?php $gyms = $pages->find("parent=/gyms/"); foreach($gyms as $gym) ?> <div class="panel panel-default panel-shadow" style="margin-bottom:10pt"> <div class="panel-body"> <h4 class="margin-t-none"> <?php $li .= "<a href='{$gym->url}'>{$gym->title}</a> $page->gymtitle "; echo $li ? "<ul>$li</ul>" : "<h1>No entries found</h1>"; ?> </h4> </div></div> Quite late at night over here in NZ. Excuse the grammar. Edited May 1, 2015 by horst added " in regard to sort query results" to subject 1 Link to comment Share on other sites More sharing options...
diogo Posted April 23, 2015 Share Posted April 23, 2015 Hi mattcohen, welcome to the forum! Quick answer here, sorry for that. Hope someone will step in with a more complete answer, but for getting you started: but for instance, what If I need to sort the items by location, etc, etc? $gyms = $pages->find("parent=/gyms/, sort=location"); Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 23, 2015 Share Posted April 23, 2015 Welcome to the forums, mattcohen. I'll keep it as short as diogo, but I hope it helps. I don't really see, why you're worried about the search results. WP_Query is kinda link ProcessWire's $pages->find() and the the looping is done by a foreach loop instead of has_posts()/the_post() combo. It's really not that different, but ProcessWire does not rely on any post specific functions to call in content. You've the whole power php gives you to sort / show / manipulate your search results. Also the combination of the selectors, which are used for $pages->find(), and custom templates is really powerful. 4 Link to comment Share on other sites More sharing options...
DaveP Posted April 23, 2015 Share Posted April 23, 2015 Welcome, mattcohen. Does WP let you sort search results by anything other than date yet? What diogo and LostKobrakai said, but I'll add a little bit. From your question, it looks like you know some php or at least prepared to learn (or you know loads). Take a look at http://processwire.com/api/selectors/. There's a lot to take in, but it will give you an idea of the power of PW selectors (especially http://processwire.com/api/selectors/#examples). We, of course, think/know PW will be a wonderful fit for your expressed needs but that's because we have been here a while. Maybe have a play with the Skyscrapers profile, on which some of those examples are based. The search page there is somewhat more detailed than in the PW install, and gives more of a feel for what you can do. 4 Link to comment Share on other sites More sharing options...
Jan Romero Posted April 23, 2015 Share Posted April 23, 2015 Hi mattcohen, since you specifically asked about “more control over the results other than using foreach”, I’d like to add to the responses above that you can absolutely sort, modify, filter the results you get from find()! What ProcessWire’s $pages->find() gives you is a special kind of array object called PageArray. PageArrays have a number of methods detailed in the ProcessWire Cheatsheet. If you need to sort the gyms from your example by location after finding them, you can do this: $gyms->sort('location'); //Sorts ascending. For descending prepend a "-" like so: sort('-location') This may be useful if, for example, you want to output the same list of gyms twice. Once sorted by rating and once by location, perhaps. You can also use find() on an existing PageArray. Maybe you want to show all gyms, but feature the three top rated ones above. You may do something like this: //Find all gyms sorted by location $gyms = $pages->find("parent=/gyms/, sort=location"); //Copy the 3 with the highest rating into $top_three (another PageArray) //No need to go to the database again, because we have $gyms already $top_three = $gyms->find("limit=3, sort=-rating"); foreach ($top_three as $top) { echo "$top->title: $top->rating Stars"; } foreach ($gyms as $gym) { echo "$gym->title in $gym->location"; } 6 Link to comment Share on other sites More sharing options...
diogo Posted April 23, 2015 Share Posted April 23, 2015 You can actually get away without using foreach now. Have a look at the last updates http://processwire.com/blog/posts/processwire-core-updates-2.5.27/ 4 Link to comment Share on other sites More sharing options...
horst Posted April 23, 2015 Share Posted April 23, 2015 How it can look like with the current dev branch version PW 2.5.27: //Find all gyms sorted by location $gyms = $pages->find("parent=/gyms/, sort=location"); //Do another find on gyms to find the top three ratings and echo output directly echo $gyms->find("limit=3, sort=-rating")->each("{title}: {rating} Stars"); // output all $gyms echo $gyms->each("{title} in {location}"); Wow, very clearly laid and less writing. 8 Link to comment Share on other sites More sharing options...
ZionBludd Posted April 24, 2015 Author Share Posted April 24, 2015 I just want to say THANK YOU to everyone who posted, it's made my day. What a great community this project has. How it can look like with the current dev branch version PW 2.5.27: //Find all gyms sorted by location $gyms = $pages->find("parent=/gyms/, sort=location"); //Do another find on gyms to find the top three ratings and echo output directly echo $gyms->find("limit=3, sort=-rating")->each("{title}: {rating} Stars"); // output all $gyms echo $gyms->each("{title} in {location}"); Wow, very clearly laid and less writing. The location tag mentioned, is that referring to a field (Custom field they are called in WP) 1 Link to comment Share on other sites More sharing options...
diogo Posted April 24, 2015 Share Posted April 24, 2015 Yes, but in pw all fields are custom. 2 Link to comment Share on other sites More sharing options...
zota Posted May 1, 2015 Share Posted May 1, 2015 Can someone add something like [ about sorting query results...] to the subject post? maybe the creator?... Link to comment Share on other sites More sharing options...
horst Posted May 1, 2015 Share Posted May 1, 2015 Can someone add something like [ about sorting query results...] to the subject post? maybe the creator?... have a look here: http://cheatsheet.processwire.com/pagearray-wirearray/sorting-and-filtering/a-sort-property/ mainly all multiple results in PW are based on wirearray and thus you can use this to sort them, or you define a sort={property} in your query selector ->find("something=something, sort=title") or sort=-date etc. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 1, 2015 Share Posted May 1, 2015 You can even sort by multiple values like this "template=posts, sort=-date, sort=title". 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now