Jump to content

Sort by checkbox status


suntrop
 Share

Recommended Posts

Hi all! I want to sort child-pages by two fields but I don't know if it is possible within one selector or if I have to make two separate queries

<?php foreach ($page->children('sort=checkbox, sort=-number') as $child): ?>

I want to keep 'checked' pages on top and sort them and all following by the 'number' field. The 'checkbox' field is a 

Link to comment
Share on other sites

Nobody loves my riddles :-) 

It should read … "The 'checkbox' field is a default checkbox field, no special module"

<?php foreach ($page->children('sort=-chiffre') as $child): ?>
	<div class="job <?= $child->highlight ? 'hightlight' : 'default' ?>">Chiffre <?= $child->chiffre ?> | <?= $child-title ?><?= $child->body ?></div>
<?php endofreach ?>

… will render …

<div class="jobs">
	<div class="job">Chiffre 734 | Job Title – Job Description …</div>
	<div class="job">Chiffre 732 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 722 | Job Title – Job Description …</div>
	<div class="job">Chiffre 720 | Job Title – Job Description …</div>
	<div class="job">Chiffre 718 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 605 | Job Title – Job Description …</div>
</div>

But what I need …

<div class="jobs">
	<div class="job highlight">Chiffre 605 | Job Title – Job Description …</div>
	<div class="job highlight">Chiffre 722 | Job Title – Job Description …</div>
	<div class="job">Chiffre 734 | Job Title – Job Description …</div>
	<div class="job">Chiffre 732 | Job Title – Job Description …</div>
	<div class="job">Chiffre 720 | Job Title – Job Description …</div>
	<div class="job">Chiffre 718 | Job Title – Job Description …</div>
</div>

… the pages with a checked "highlight" (the checkbox field) should be on top and ordered by the chiffre. The other pages should follow and ordered by chiffre too.

  • Like 1
Link to comment
Share on other sites

From the docs:

 

Quote

You may specify multiple fields to sort by. For example, lets say that you wanted to sort first by date descending (newest to oldest) and then by title ascending, you would do this:


sort=-date, sort=title
 

 

Link to comment
Share on other sites

Hm, okay. Maybe then make two arrays and merge them / prepend the array with checkboxes?

<?php 
$withcheckbox = $page->children("checkboxname=1"); 
$others = $page->children("checkboxname=0, sort=-chiffre");
$all = $withcheckbox->import($others);

foreach($all as $a) {
..
?>

 

  • Like 1
Link to comment
Share on other sites

I thought it is possible within one selector … and I only have to rewrite my selector. 

My other solution looks better :-)

$page->children('highlight=1, sort=-chiffre')->add( $page->children('highlight=0, sort=-chiffre') )

 

Link to comment
Share on other sites

1 hour ago, suntrop said:

The problem is it doesn't seem to work with checkboxes.

It's working for me. I assume the code in your first post is just for demonstration, but double-check that you are using the name of your checkbox field in the selector, i.e. "sort=highlight" and not "sort=checkbox". And you will want to reverse the sort for the checkbox to have checked items appear first: "sort=-highlight".

  • Like 1
Link to comment
Share on other sites

HI Robin. Thanks for checking. But I can't get this to work, double checked my wording. I am using PW 3.0.78. Perhaps we are using other "checkbox modules"? How can I tell what module this checkbox field is exactly? I am pretty sure I have not installed any module like this … but possibly I am wrong :) 

Link to comment
Share on other sites

14 hours ago, suntrop said:

Perhaps we are using other "checkbox modules"?

I tested with the core Checkbox fieldtype. You can check the fieldtype by opening the field (Setup > Fields > Your field) and looking in the Type dropdown.

2017-10-27_165014.png.1db54ac2ae2f14bacf32f7be27795b9b.png

Not sure why it wouldn't work for you, but there is no real harm in just sticking with what you are doing (combining two PageArrays together) unless you have a huge number of children. 

  • 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

×
×
  • Create New...