Skip to content

Commit

Permalink
(feat): enable searching by post id
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin authored and Colin committed Dec 13, 2023
1 parent 28f46d8 commit 2286b51
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Leges/PostType/LegesPostTypeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function register()
{
$this->plugin->loader->addAction('init', $this, 'registerPostType');

$this->plugin->loader->addFilter("ext-cpts/{$this->postType}/filter-query/{$this->prefix}_post_id", $this, 'filterByPostId', 10, 3);

if (class_exists('\WP_CLI')) {
\WP_CLI::add_command('owc-update-leges', [UpdatePrices::class, 'execute'], ['shortdesc' => 'Update lege prices when specified date has been reached.']);
}
Expand Down Expand Up @@ -117,8 +119,25 @@ public function registerPostType()
'date_format' => 'd M Y'
],
],
'admin_filters' => [
"{$this->prefix}_post_id" => [
'title' => 'ID',
'meta_search_key' => '', // Use the "meta_search_key" type in order to generate a text input
],
],
];

return register_extended_post_type($this->postType, $args, $labels);
}

public function filterByPostId($return, $query, $filter): array
{
$postId = trim($query["{$this->prefix}_post_id"] ?? '');

if ($postId) {
$return['p'] = is_numeric($postId) ? (int)$postId : -1; // Non-numeric input should return no posts
}

return $return;
}
}

0 comments on commit 2286b51

Please sign in to comment.