101 lines
3.0 KiB
PHP
101 lines
3.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
Enhances the post management for 'sp_event' custom post type by adding custom
|
|
filters for events with or without outcomes
|
|
*/
|
|
|
|
/**
|
|
* Generates a meta query argument array for filtering 'sp_event' custom posts based on the presence or absence of outcomes.
|
|
*
|
|
* @param bool $has_outcome Whether to filter events with outcomes (true) or without outcomes (false). Default is true.
|
|
*
|
|
* @return array Meta query argument array for WP_Query.
|
|
*/
|
|
function get_sp_event_has_outcome_meta_query_args($has_outcome = true): array
|
|
{
|
|
if ($has_outcome) {
|
|
return [
|
|
"relation" => "AND",
|
|
[
|
|
"key" => "sp_results",
|
|
"value" => '"outcome"',
|
|
"compare" => "LIKE",
|
|
],
|
|
[
|
|
"key" => "sp_results",
|
|
"value" => 's:7:"outcome";a:0:{}', // i.e. a blank outcome
|
|
"compare" => "NOT LIKE",
|
|
],
|
|
];
|
|
} elseif (!$has_outcome) {
|
|
return [
|
|
"relation" => "OR",
|
|
[
|
|
"key" => "sp_results",
|
|
"value" => '"outcome"',
|
|
"compare" => "NOT LIKE",
|
|
],
|
|
[
|
|
"key" => "sp_results",
|
|
"value" => 's:7:"outcome";a:0:{}', // i.e. a blank outcome
|
|
"compare" => "LIKE",
|
|
],
|
|
];
|
|
}
|
|
}
|
|
|
|
// Add the custom filter dropdown
|
|
function outcome_filter_dropdown()
|
|
{
|
|
$current_screen = get_current_screen();
|
|
|
|
if ($current_screen->id == "edit-sp_event") {
|
|
if (isset($_GET["has-outcome"])) {
|
|
switch ($_GET["has-outcome"]) {
|
|
case 'true':
|
|
$selected="has-outcome";break;
|
|
case 'false':
|
|
$selected="has-no-outcome";break;
|
|
case '':
|
|
$selected='';break;
|
|
}
|
|
}; ?>
|
|
<select name="has-outcome">
|
|
<option value="" <?php selected(
|
|
"",
|
|
$selected
|
|
); ?>>All Outcomes</option>
|
|
<option value="false" <?php selected(
|
|
"has-no-outcome",
|
|
$selected
|
|
); ?>>Missing Outcome</option>
|
|
<option value="true" <?php selected(
|
|
"has-outcome",
|
|
$selected
|
|
); ?>>Has Outcome</option>
|
|
</select>
|
|
<?php
|
|
}
|
|
}
|
|
|
|
add_action("restrict_manage_posts", "outcome_filter_dropdown");
|
|
|
|
// Modify the query based on the selected filter
|
|
function outcome_filter_query($query)
|
|
{
|
|
global $pagenow;
|
|
|
|
if ($pagenow == "edit.php" && isset($_GET["has-outcome"]) ) {
|
|
if ($_GET["has-outcome"] == "false") {
|
|
$meta_query = get_sp_event_has_outcome_meta_query_args(false);
|
|
$query->set("meta_query", $meta_query);
|
|
}
|
|
elseif ($_GET["has-outcome"] == "true") {
|
|
$meta_query = get_sp_event_has_outcome_meta_query_args(true);
|
|
$query->set("meta_query", $meta_query);
|
|
}
|
|
}
|
|
}
|
|
|
|
add_action("pre_get_posts", "outcome_filter_query");
|