Home › Forums › Backend Issues (wp-admin) › Limit post object to author inside user-edit.php (backend user profile)
Hi community,
in a frontend form I limit the output of a post object to the author like this:
/** Reduce ACF Post Object Select of Featured Product to Current User */
function my_post_object_query( $args, $field, $post ) {
// modify the order
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$args['author']=$user_id;
$args['authors']=$user_id;
$args['post_type']='product';
return $args;
}
// filter for every field
add_filter('acf/fields/post_object/query/name=featured_product', 'my_post_object_query', 10, 3);
For the administrator I want to limit the same post object in the profile page of the author (user-edit.php) by not getting the current user ID but the ID of the user profile the admin is looking at like this:
global $profileuser;
$user_id = $profileuser->ID;
I can retrieve the ID but I cannot get it to work within the following post object function:
/** Reduce ACF Post Object Select of Featured Product to Author */
function my_post_object_query( $args, $field, $post ) {
// modify the order
global $profileuser;
$user_id = $profileuser->ID;
$args['author']=$user_id;
$args['authors']=$user_id;
$args['post_type']='product';
return $args;
}
// filter for every field
add_filter('acf/fields/post_object/query/name=featured_product', 'my_post_object_query', 10, 3);
What works is if I ask for it statically like this:
....
$user_id = 2;
$args['author']=$user_id;
....
Do you have any suggestions how I could retrieve the User-ID within the function?
Any ideas would be helpful.
The problem is that the values are loaded using AJAX and during the AJAX request $profileuser probably does not contain any value.
The 3rd argument that’s passed to your function should have the use ID, but I don’t really know what’s in the argument.
you can find this out. Turn on error logging in WP by adding this to wp-config.php
define('WP_DEBUG', true );
define('WP_DEBUG_DISPLAY', true);
define('WP_DEBUG_LOG', true);
then in your filter do this
ob_start();
print_r($post);
error_log(ob_get_clean());
Go to your user profile page, then you can look in the error log ‘/wp-contnet/error.log’ to see what get written to the file.
Hi John,
thank you for your help. I decided I ask on wpquestions.com since it was a rather complex thing and Reigel Garllade came up with a good solution.
Source: http://www.wpquestions.com/question/showChrono/id/15258
I’ll share it here, in case anybody will ever run into the same issue. For me the issue is solved, but I am curious if you have objections to the following code.
The following code goes into the functions.php
add_action('acf/input/admin_footer','admin_footer');
function admin_footer(){
?>
<script>
/* <![CDATA[ */
acf.add_filter('select2_args',function( args ) {
if ( typeof args.ajax.data == 'function' ) {
var old_data_func = args.ajax.data; // We'll keep this for maximum compatibility, and extend it.
args.ajax.data = function(term, page) {
var default_response = old_data_func( term, page ); // Call the old, default function.
// Add the user_id to the ajax function.
default_response.user_id = function () {return <?php echo isset($_GET['user_id'])?$_GET['user_id']:''; ?>;};
// Return the default args with our user_id function.
return default_response;
}
}
return args;
}
);
/* ]]> */
</script>
<?php
}
and then:
add_filter('acf/fields/post_object/query/name=featured_product', 'my_post_object_query', 10, 3);
function my_post_object_query( $args, $field, $post ) {
// modify the order
if( isset($_POST['user_id']) && is_numeric($_POST['user_id'])) {
$user_id = $_POST['user_id'];
} else {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$args['author']=$user_id;
$args['authors']=$user_id;
$args['post_type']='product';
return $args;
}
hello Laika,
I also found out that you can do something like this that does the same thing…
add_action('acf/input/admin_footer','admin_footer');
function admin_footer(){
?>
<script>
acf.add_filter(
'prepare_for_ajax',
function(a) {
if (a.action === 'acf/fields/post_object/query') {
a.user_id = <?php echo isset($_GET['user_id'])?$_GET['user_id']:''; ?>;
}
return a;
}
);
</script>
<?php
}
Thank you Reigel. That looks really economic, I will try it.
My deepest respect!
The topic ‘Limit post object to author inside user-edit.php (backend user profile)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.