106 lines
3.2 KiB
PHP
106 lines
3.2 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_filter("views_edit-sp_event", "wp37_sp_event_does_not_have_outcome_filter");
|
|
function wp37_sp_event_does_not_have_outcome_filter($views)
|
|
|
|
{
|
|
if (is_admin() && $_GET["post_type"] == "sp_event") {
|
|
global $wp_query;
|
|
$filter_name = "Missing Results";
|
|
|
|
$result = new WP_Query([
|
|
"post_type" => "sp_event",
|
|
"meta_query" => [get_sp_event_has_outcome_meta_query_args(false)],
|
|
]);
|
|
|
|
// $link = add_query_arg("sp_event_has_outcome", "false");
|
|
$link="#";
|
|
// $link = admin_url( "edit.php?post_type=sp_event&sp_event_has_outcome=false" );
|
|
|
|
$sp_event_has_outcome =
|
|
get_query_var("sp_event_has_outcome") === "false";
|
|
$class = $sp_event_has_outcome === true ? ' class="current"' : "";
|
|
$views["sp_event_does_not_have_outcome"] = sprintf(
|
|
'<a href="%s"' . $class . ">" . $filter_name . " (%d)",
|
|
$link,
|
|
$result->found_posts
|
|
);
|
|
|
|
return $views;
|
|
}
|
|
}
|
|
|
|
add_action("init", "wpse246143_register_sp_event_has_outcome");
|
|
function wpse246143_register_sp_event_has_outcome()
|
|
{
|
|
global $wp;
|
|
$wp->add_query_var("sp_event_has_outcome");
|
|
}
|
|
|
|
add_action("parse_query", "wpse246143_map_sp_event_has_outcome");
|
|
function wpse246143_map_sp_event_has_outcome($wp_query)
|
|
{
|
|
if (
|
|
isset($wp_query->query["post_type"]) and
|
|
$wp_query->query["post_type"] == "sp_event" and
|
|
$wp_query->get("sp_event_has_outcome")
|
|
) {
|
|
$sp_event_has_outcome =
|
|
get_query_var("sp_event_has_outcome") === "true";
|
|
|
|
if ($sp_event_has_outcome) {
|
|
$meta_query = get_sp_event_has_outcome_meta_query_args(true);
|
|
} elseif (!$sp_event_has_outcome) {
|
|
$meta_query = get_sp_event_has_outcome_meta_query_args(false);
|
|
}
|
|
|
|
$wp_query->set("meta_query", $meta_query);
|
|
}
|
|
}
|
|
|