awards-php-query.php

Award Sorting Functions

These PHP functions are designed to fetch and sort custom WordPress award posts. It uses an array of predefined categories and types to sort awards by year, category, and then by type, in that order.

<?php // Function to sort awards function codewp_sort_awards($a, $b) { // Define your specific order for categories and types $categories_order = array('Best Actor', 'Best Actress', 'Best Picture'); $types_order = array('Nominee', 'Winner'); // Compare by year if ($a->awd_year != $b->awd_year) { return $a->awd_year - $b->awd_year; } // Compare by category $a_category_index = array_search($a->awd_sort_cat, $categories_order); $b_category_index = array_search($b->awd_sort_cat, $categories_order); if ($a_category_index != $b_category_index) { return $a_category_index - $b_category_index; } // Compare by type $a_type_index = array_search($a->awd_type, $types_order); $b_type_index = array_search($b->awd_type, $types_order); if ($a_type_index != $b_type_index) { return $a_type_index - $b_type_index; } return 0; } // Fetch and sort awards function codewp_fetch_and_sort_awards() { // Fetch awards $args = array('post_type' => 'awards', 'numberposts' => -1); $awards = get_posts($args); // Sort awards usort($awards, 'codewp_sort_awards'); return $awards; } $sorted_awards = codewp_fetch_and_sort_awards(); ?>

Frequently Asked Questions

This code defines two functions for sorting awards in WordPress posts by year, category, and type.