Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Order posts by ACF checkbox WordPress

  • SOLVED

Hi There,

I have a custom post type called ‘Skateboards’.

With in the custom post type of ‘Skateboards’ , I added three checkboxes in the edit post page via the ACF plugin.

The three check boxes are ‘Great’ , ‘Not So Great’ and ‘Terrible’.

What I am trying to accomplish is to display the ‘Skateboards’ post type in order of ‘Great’ , ‘Not So Great’ and ‘Terrible’.

So all of the Skateboards posts marked with ‘Great’ checkbox will display 1st. Followed by all Skateboards posts marked with ‘Not So Great’ checkbox to display 2nd and then all skateboards posts marked with ‘Terrible’ to display 3rd.

I would need the args query and the wp-query to display the posts.

Please let me know if you have any additional questions.

Thanks again for taking a look!

Answers (4)

2022-04-27

Naveen Chand answers:

Great, Not So Great and Terrible... coincidentally, these words happens to be arranged in ascending order. So taking advantage of this, we can simply use this query:


// get posts
$posts = get_posts(array(
'post_type' => 'skateboards', //insert the correct post_type name
'posts_per_page' => -1,
'meta_key' => 'skateboard_performance', //sample acf field name
'orderby' => 'meta_value',
'order' => 'ASC'
));


I have assumed 'skateboard_performance' as the name of the field as an example. You may replace it with the correct field name.

If you have setup three separate fields (one each for great, not-so-great and terrible), then the above answer will not work. Do let me know if that is the case.

On the other hand, if you have you have used one field_name with checkbox type that has three options (great, not-so-great and terrible), then the above code should work without any issue.


User184831 comments:

Hi Naveen,

Thanks for taking a look into this question.

I did use one field name with a checkbox type that has three options in ACF. (Using the 'skateboard_performance' as a field name would be correct.) Then the

But how would the query go if by chance the names of the checkbox fields were not coincidentally in asc order?

For e.g

Field Name :
skateboard_performance

Field Type:
Checkbox

Choices: Value-Label:

platinum:Platinum
gold:Gold
silver:Silver


Naveen Chand comments:

Yeah, if the order of the checkbox option names are not in asc order, we should sort them and then loop through each of the option names.

But to get the checkbox option names, we need to get the field key which is not same as our meta_key 'skateboard_performance'. ACF stores field key separately. To get it, when you are on the Edit Field Groups page, simply go to Screen Options on top right corner and then enable Field Key. Once you get it, you can then extract the checkbox options and sort them.

Please see this code that does exactly what I mentioned above:


//get this from the Edit Field Group screen by clicking on Screen Options at the top right corner and then Show Field Key: Yes. It will be something like this 'field_3939393edi4kk3'. In my case, it is field_626a31c5e2e7b:

$field_key = 'field_626a31c5e2e7b';

$field = get_field_object($field_key);

//all the choices will be in this array: $field['choices'].
$choices_array = $field['choices'];

//sort these choices by key - this will sort them in ascending order.
ksort($choices_array);

//now that choices are in ascending order,
//loop through the choices and call wp query on every iteration.

foreach( $choices_array as $name => $label ) {

echo "<strong>Skateboards with performance rating: " .$label ."</strong><br>";

$skateboard_posts = new WP_Query( array(
'post_type' => 'skateboard', //insert the correct post_type name
'posts_per_page' => -1,
'meta_key' => 'skateboard_performance', //sample acf field name
'meta_value' => serialize(array($name)),
)
);
if ( $skateboard_posts->have_posts() ) {
while ( $skateboard_posts->have_posts() ) {
$skateboard_posts->the_post();
echo the_title() ."<br>";
}
} //end if
wp_reset_postdata();
} //end foreach


I have wrapped the above code in a function and used it as shortcode like this:


add_shortcode('getposts_sortby_acf_values', 'getposts_sortby_acf_values');
function getposts_sortby_acf_values(){
//get this from the Edit Field Group screen by clicking on Screen Options at the top right corner and then Show Field Key: Yes. It will be something like this 'field_3939393edi4kk3'. In my case, it is field_626a31c5e2e7b:

$field_key = 'field_626a31c5e2e7b';

$field = get_field_object($field_key);

//all the choices will be in this array: $field['choices'].
$choices_array = $field['choices'];

//sort these choices by key - this will sort them in ascending order.
ksort($choices_array);

//now that choices are in ascending order,
//loop through the choices and call wp query on every iteration.

foreach( $choices_array as $name => $label ) {

echo "<strong>Skateboards with performance rating: " .$label ."</strong><br>";

$skateboard_posts = new WP_Query( array(
'post_type' => 'skateboard', //insert the correct post_type name
'posts_per_page' => -1,
'meta_key' => 'skateboard_performance', //sample acf field name
'meta_value' => serialize(array($name)),
)
);
if ( $skateboard_posts->have_posts() ) {
while ( $skateboard_posts->have_posts() ) {
$skateboard_posts->the_post();
echo the_title() ."<br>";
}
} //end if
wp_reset_postdata();
} //end foreach
} //end of function


We can now use the shortcode [getposts_sortby_acf_values] in any page and it will display the skateboard post_titles grouped by the act field option names.

I have tried it on my test site and it produces the desired result of showing posts grouped by acf field choices that are ordered in ascending.

Hope you will find it useful.

thanks.


User184831 comments:

Thank you so much Naveen! - I had noooooo idea about the form group field key that you mentioned - This was very helpful and thanks again for showing me this. Very much appreciated!!

2022-04-27

Mohamed Ahmed answers:

This code will do it for meta key "skateboard_performance" order by
-Great
-Not So Great
-Terrible


$Skateboards = get_posts( array(
'post_type' => 'Skateboards',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids', // get just the ID's of posts
'meta_key' => 'skateboard_performance',
'orderby' => 'meta_value_list',
'meta_value_list' => array( 'Great', 'Not So Great', 'Terrible'),
'suppress_filters' => false,
) );

add_filter( 'posts_orderby', 'prowpsite_posts_orderby_meta_value_list', 10, 2 );
function prowpsite_posts_orderby_meta_value_list( $orderby, $query ) {
$key = 'meta_value_list';
if ( $key === $query->get( 'orderby' ) &&
( $list = $query->get( $key ) ) ) {
global $wpdb;
$list = "'" . implode( wp_parse_list( $list ), "', '" ) . "'";
return "FIELD( $wpdb->postmeta.meta_value, $list )";
}

return $orderby;
}

2022-04-28

Cesar Contreras answers:

Have you considered using a Taxonomy instead?
If so, I share the code to add it to your Custom Post Type.
I think it will work better for you because it is used like the categories.

Add this code to your functions.php file.


/**
* Create taxonomies "performance" for the post type "skateboards".
*/
function wpdocs_create_skateboards_performance_taxonomie() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Performances', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Performance', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Performances', 'textdomain' ),
'all_items' => __( 'All Performances', 'textdomain' ),
'parent_item' => __( 'Parent Performance', 'textdomain' ),
'parent_item_colon' => __( 'Parent Performance:', 'textdomain' ),
'edit_item' => __( 'Edit Performance', 'textdomain' ),
'update_item' => __( 'Update Performance', 'textdomain' ),
'add_new_item' => __( 'Add New Performance', 'textdomain' ),
'new_item_name' => __( 'New Performance Name', 'textdomain' ),
'menu_name' => __( 'Performance', 'textdomain' ),
);

$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'performance' ),
);

register_taxonomy( 'performance', array( 'skateboards' ), $args );

}
// hook into the init action and call wpdocs_create_skateboards_performance_taxonomie when it fires
add_action( 'init', 'wpdocs_create_skateboards_performance_taxonomie', 0 );

2022-04-29

Echeverri answers:

you wish manage all from the WP admin? or if is possible you can considering manage the performance order from code