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!
Solution - 1
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.
Solution - 2
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;
}
Solution - 3
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 );
Solution - 4
you wish manage all from the WP admin? or if is possible you can considering manage the performance order from code
|