Ask your WordPress questions! Pay money and get answers fast! (more info)

Limit output of ACF post object to user_id in user-edit.php

  • SOLVED

Hi WP Experts,

in a frontend form I can filter the output of posts based on the author in ACF post object 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);
fig.: Code 1



Sources:

[[LINK href="http://www.advancedcustomfields.com/resources/post-object/"]]http://www.advancedcustomfields.com/resources/post-object/[[/LINK]]
[[/LINK]]
[[LINK href="http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
"]]http://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
[[/LINK]]


For the administrator view I want to filter the same post object in the profile page of the author (user-edit.php) by not getting the current user ID (that would filter the Admin) but the ID of the user profile the admin is looking at.

I can extract the authors user ID from the URL:

$user_id = $GET_['user_id']

or use the global $profileuser variable:

global $profileuser;
$user_id = $profileuser->ID;



The ID is shown when I echo it out, but as soon as I integrate in the ACF function, it is not working to filter the posts by this user ID.

/** Reduce ACF Post Object Select of Featured Product to Author */

function admin_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', 'admin_post_object_query', 10, 3);

fig.: Code 2

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.

Looking forward to your ideas.

Answers (1)

2016-02-12

Reigel Gallarde answers:

change your function to this.. with this function... we are trying to get the user_id and fallback to current user id if $_GET['user_id'] is not a number..
function admin_post_object_query( $args, $field, $post ) {
// modify the order

if( isset($_GET['user_id']) && is_numeric($_GET['user_id'])) {
$user_id = $_GET['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;

}


this will not work on admin side though... because it uses ajax to POST the values and get the data of the select by using admin_ajax.php...
in the file or url admin_ajax.php our $_GET is empty... so $_GET['user_id'] will not work.

After hours of tinkering the plugin, I can't find ways to change the data being submitted.. I was thinking we could include user_id in the POST data of the ajax.. but there's no filter or action for it.

so here's what I've got so far... we can try to change the url for the ajax and include the user_id... like this..

add_action('acf/input/admin_footer_js','admin_footer_js');
function admin_footer_js(){
// options
$o = array(
'ajaxurl' => admin_url( 'admin-ajax.php?user_id='.$_GET['user_id'] ),
);
?>
acf.tmp = <?php echo json_encode($o); ?>;
acf.o.ajaxurl = acf.tmp.ajaxurl
<?php
}


the ajax url would now be something like admin-ajax.php?user_id=2. You can now use $_GET.


Reigel Gallarde comments:

Ahh here's another solution without changing the ajax url...

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
}

this will add user_id to the POST data of the ajax request...


and here's your php function... take note that I'm using your my_post_object_query function... this function should work both admin and front end...

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;

}


Jan-Philipp Wittrin comments:

Wow Reigel,

the second one is the correct answer. I am so lucky that I asked here.

I don't know how to mark your answer as correct answer. I somehow seem to not have sufficent capabilities to do that. Maybe you know what i might need to do.


You really earned your money, and I want you to have it asap.

Thank you so much again.


Reigel Gallarde comments:

You're welcome... click [[LINK href="http://www.wpquestions.com/question/pickAWinner/id/15258"]]Vote to award prize[[/LINK]]


Jan-Philipp Wittrin comments:

Done :-)