Jump to content

Filter page results by Page Reference Field value (Page AutoComplete)


MilenKo
 Share

Recommended Posts

Hello all.

A few days ago I decided to redo my first PW theme (knowledge sharing one) trying to apply some more elegant logic and most important - to learn to filter page results by a Page reference field with some values. So far it is all going well except the fact that I am now at a stage to add some page parameters and am a bit stuck as to how to filter them.

Here is an example of the structure planned to use:

!-- Articles (template = articles.php)
!---- Article 1 (template = articles-inner.php)
!---- Article 2 ..
!---- Article 3 ..

!-- Categories (no template file)
!---- Category1 (same template as  parent)
!---- Category2 ..
!---- Category3 ..

!-- OS (no template file)
!---- Windows (same template as parent)
!---- Linux
!---- MacOS

So far the home page and articles-inner pages are fully working. I need to show in the sidebar two blocks; Categories, OS where clicking on one would show only the posts containing the PageReference field value. To list the categories and OS in the block won't be a problem as I would just need to query the parent and list all the children (if any) with the proper markup. However, how to have the Categories and OS blocks link only to the specific posts that is the issue. In my previous version of the knowledge profile, I had the "categories" listed as a parent and the posts matching the category were just children of it. But now my goal is to learn how to add different parameters using Page Reference and have a way to show the results per the specific value.

How would you organize a similar structure and what approach to take to have a simple and elegant solution?

Or, if you know of a PW profile that I could dissect and learn the filtering mechanism of Page Reference etc. it would be even better. I am presently looking at the Ghost profile however Wire-approach is still not very much clear to me, unfortunately.

 

Link to comment
Share on other sites

You can put the filter logic in your articles.php template:

// First Approach
//Allow URL segments on the articles template
// so the url will be example.com/articles/cars/

1 - $category = $sanitizer->selectorValue($input->urlSegment1);

// Second Approach
// If you don't want to use segments, you can use querystrings, but getting a less refined approach IMHO
// example.com/articles/?cat=cars

2- $category = $sanitizer->entities($input->get->cat);

$filtered_articles = pages()->get('/articles/')->children("categories=$category, limit=10");

 

  • Like 2
Link to comment
Share on other sites

Thank you very much for the approaches @Sergio. I think that I could try the approach 1 for the sidebar links but in a case of the advanced filter, the approach 2 would fit better for multiple criteria. So approach 2 would be:

// example.com/articles/?cat=category1&os=windows

//Sanitize the category value from the url
$category = $sanitizer->entities($input->get->cat);

//Sanitize the OS value from the url
$os = $sanitizer->entities($input->get->os);

//Filter the results by the two values in the url
//categories and os are the fields attached to articles inner page
$filtered_articles = pages()->get('/articles/')->children("categories=$category, os=$os, limit=10");

So now I will only have to apply the logic to articles.php and test it in action. Thanks again!

Link to comment
Share on other sites

OK, so I added the segments to the links and now it is the turn to implement the logic. Before I proceed and add the logic, I was wondering what would be the best practice to add the segments. Presently I do not have a template for categories nor for tags, so I could either create two and have a URL like:

http://exampledomain.com/category/Category1  |  http://exampledomain.com/tag/Tag1

For the moment I have a template for listing all articles (articles.php) so if I insert the logic in it, the URL would become:

http://exampledomain.com/articles/category/Category1  | http://exampledomain.com/tag/Tag1

For the segment part it does not matter, however, I am not sure if it does matter for search bots and better ranking. Any input on that?

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

×
×
  • Create New...