Support

Account

Home Forums Backend Issues (wp-admin) Limit post object to author inside user-edit.php (backend user profile) Reply To: Limit post object to author inside user-edit.php (backend user profile)

  • 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;
    
    }