Created
January 24, 2019 16:18
-
-
Save pierre-dargham/329c95b5a551912ee6d8a9bd5b7b9a4d to your computer and use it in GitHub Desktop.
wp-cubi-acf-post-object-field-with-id.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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