Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pierre-dargham/329c95b5a551912ee6d8a9bd5b7b9a4d to your computer and use it in GitHub Desktop.
Save pierre-dargham/329c95b5a551912ee6d8a9bd5b7b9a4d to your computer and use it in GitHub Desktop.
wp-cubi-acf-post-object-field-with-id.php
<?php
/*
* Plugin Name: ACF Post Object Field With ID
* Description: Show & search by ID with acf post object field
* Author: Pierre Dargham
* Author URI: https://github.com/pierre-dargham/
* Version: 1.0
*/
namespace Globalis\WP\Cubi;
add_filter('acf/fields/post_object/result', __NAMESPACE__ . '\\acf_post_object_title_show_ID', 10, 4);
add_filter('acf/fields/post_object/query', __NAMESPACE__ . '\\acf_post_object_search_by_ID', 10, 3);
function acf_post_object_title_show_ID($title, $post, $field, $post_id)
{
return $post->ID . ' : ' . $title;
}
function acf_post_object_search_by_ID($args, $field, $post_id)
{
if (!empty($args['s']) && is_numeric($args['s']) && strval(intval($args['s'])) == strval($args['s'])) {
$search_id = $args['s'];
$args['post__in'] = [$search_id];
unset($args['s']);
}
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment