Jump to content

Is PW a "better" fit for my needs in regard to sort query results?


ZionBludd
 Share

Recommended Posts

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:
 
Homepage
Simple static page with a hero/search box, etc
 
Search Results Page
One 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 Page
Displaying 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 by horst
added " in regard to sort query results" to subject
  • Like 1
Link to comment
Share on other sites

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

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.

  • Like 4
Link to comment
Share on other sites

Welcome, mattcohen.

Does WP let you sort search results by anything other than date yet? :lol:
 
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.

  • Like 4
Link to comment
Share on other sites

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";
}
  • Like 6
Link to comment
Share on other sites

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

  • Like 8
Link to comment
Share on other sites

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)

  • Like 1
Link to comment
Share on other sites

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.

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