Filter Page List by a custom select attribute

Permalink 1 user found helpful
I have a custom select attribute named Program.
Program has the options:
Animal Science
Agricultural Engineering Technology, etc...

I'm trying to create a custom template for the page list block that will only show pages that have a specific option selected from the program attribute, for instance "Animal Science".

So I thought that this line adapted from the documentation would filter my page list but unfortunately it hasn't.

$pl->filterByAttribute('program', 'animal_science');

I've searched the forums and turned up some interesting discussions but they all seem to be not quite what I need or WAY over my head. I'm not even sure where to put the above code (put it in the controller above the other filters, didn't do anything).

I've been racking my brain over this for the past two days so any help would be greatly appreciated.

View Replies:
jordanlev replied on at Permalink Reply
You can't really perform filtering in the custom template -- by the time the template is being displayed it's too late (all the page data has already been retrieved in the controller).

You might want to try this free block I posted to another forum thread which is very similar to what you want to do (but I called it "Related Content" instead of "Program"):
I think if you install that and then go to Dashboard -> Pages and Themes -> Attributes, and change the name of the "Related Content" attribute to "Program" (but leave the handle as-is), that should do the trick. If you already have the Programs entered into the content of your site (like you already built out the site and assigned programs to pages), then it's going to be more challenging -- will require altering the code of that addon.

Also there's a paid addon that does a similar thing, which might be a bit more polished than the free one I linked to (but I haven't personally used it myself so I'm not sure what its capabilities are):
brothersjef replied on at Permalink Reply
Jordan, Thanks for the reply. I actually saw that thread and installed the related content block. I added it to my page but it did not display any content.
beebs93 replied on at Permalink Reply
Dumb question time:

If one of the "program" page attribute values is "Animal Science", but you filter for "animal_science" how would that work since select value handles aren't based on the values themselves?

Shouldn't you use?:

$pl->filterByAttribute('program', 'Animal Science');

EDIT: BTW, Jordan is right, making a custom template wouldn't help for the reasons he stated. Unless you want to programatically make the block or manually utilize the PageList model yourself, you're out of options.
jordanlev replied on at Permalink Reply
Yes you're right, you can't filter on the attribute handle but rather on the value name. Also you must surround the value with newline characters ("\n"). Overall, querying on a select attribute is very tricky and took me a while to figure out. If you're interested in understanding the proper way to do these kinds of queries, check out that thread I linked to (and also the code itself in that related_content_links block I posted there).
brothersjef replied on at Permalink Reply
The controller file filters what is displayed through the view file correct? so isn't there a way to simply add a filter to the controller that will allow only pages with a certain attribute to be passed to the view?
brothersjef replied on at Permalink Reply
OK, I added:
$pl->filterByAttribute('program', "\nAnimal Science\n");

to the controller of the page list block and it worked. I guess i may just have to take the plunge and figure out how to make a custom block. The project I'm working on is for the university where I work and I wanted to get this up and running with out having to go through the red tape of requisitioning 20 bucks or whatever to pay for an add-on.
Thanks for the help and if anyone has any other suggestions I would appreciate it.