OpenBayou Posted November 7, 2016 Share Posted November 7, 2016 Hi, I have a select field called 'show' and it has two options to select: 1=CTRL Daily 2=Channelgregnet How do you filter posts by the select field? Thanks. Link to comment Share on other sites More sharing options...
adrian Posted November 7, 2016 Share Posted November 7, 2016 Both of these will find the same pages: $pages->find("show=1")); $pages->find("show.label=CTRL Daily"); 2 Link to comment Share on other sites More sharing options...
OpenBayou Posted November 7, 2016 Author Share Posted November 7, 2016 So I would do: <?php if ($pages->find("show.label=CTRL Daily")):?> code <?php endif;?> Link to comment Share on other sites More sharing options...
adrian Posted November 7, 2016 Share Posted November 7, 2016 I am not sure exactly what you want to do, but if for example you want to show the 10 most recent posts with their titles and summaries, I would use the selector to get the pages/posts as the children of the Posts parent, so something like this: foreach($page->children("show=1, limit=10, sort=-published_date") as $post) { // output $post->title, $post->summary etc for each post } Is that what you are looking for? BTW, I would use the key of the "show" options field, rather than the label, but it's really up to you. 1 Link to comment Share on other sites More sharing options...
OpenBayou Posted November 8, 2016 Author Share Posted November 8, 2016 Thanks for the help. It's what I was looking for. <?php foreach($page->children("show.label=CTRL Daily") as $post) :?> <?php echo $post->title;?> <?php echo $post->ad_copy;?> <?php endforeach;?> 1 Link to comment Share on other sites More sharing options...
szabesz Posted November 8, 2016 Share Posted November 8, 2016 I always do something like this: $pages = $page->children("show.label=CTRL Daily") if(count($pages) && is_array($pages)) { foreach($pages as $post) //etc... } else { error_log("Custom NOTICE: Options Fieldtype called XYZ contains no label 'CTRL Daily'!"); } This way you will have a clue what goes on if someone (including you) happens to rename the option. Note, that even using IDs has its drawback: the order of labels can be changed as well even if they are exactly the same. 1 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