-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storing data as array #7
Comments
Hi Ryan, can you give me an example. I've not had this feedback before. I'm |
Sure thing! I've got a "User home page" where I would like to query a list of "Instructions" (custom post type) specific to each user. global $current_user;
$current_user = wp_get_current_user();
echo $userID = $current_user->ID;
$args = array(
'numberposts' => -1,
'post_type' => 'instructions',
'meta_key' => 'user',
'meta_value' => $userID
); At the moment there are no matches because the metadata is an Array, the only hacky fix is to change the query to a LIKE and have user id 1 match user id 11 etc. I understand rolling out this change now is too risky, perhaps you would be able to add an option to specify the output much like the current Image field works? As I understand it it will store the ID and then modify the output depending on the option, you could add the option to choose between a user ID or a user Array. Keeping Array as the default to avoid any issues. |
Thats sounds like a really good addition. I am assuming you are only storing as single user... A quick fix for you just now ryan would be to add a function on save post Something like ... add_action('save_post', acf_user_field_quickfix', 1, 2);
/**
* Fired when page saved
*
* @return void
* @author Lewis Carey
*/
function acf_user_field_quickfix($post_id, $post) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }
if(($_POST['post_type'] == 'instructions') &&
(!current_user_can('edit_page', $post_id))) {
return $post_id;
} elseif(!current_user_can('edit_post', $post_id)) {
return $post_id;
}
if($_POST['post_type'] != 'instructions') {
return $post_id;
}
if($post->post_type == 'revision') return;
//inspect element to find out name of the user select field on your
edit post page to replace XXXXXXXXX
$userID = $_POST['fields']['field_XXXXXXXX'];
delete_post_meta($post_id, 'instruction_user');
if(isset($userID)) {
add_post_meta($post->ID, 'instruction_user', $userID);
}
} You should be able to then query meta_key 'instruction_user'. Disclaimer: The code above may need tinkering with as I havent tested it. |
Brilliant, thanks for the head start! Yeah I'm only storing a single user, as with any field that allows you to select multiple values it seems to be a common limitation with the way Wordpress queries posts and not the field itself, in most cases you can solve it by changing the object you define the relationship on, but as acf doesn't yet support adding meta boxes to user edit pages (this query problem could be easily fixed with a post relationship field on a user) I have to work around it. Thanks heaps for the help and great addon! |
No worries, I have had a few issues since the translation update so I am looking at it afresh shortly - thanks for the feedback and I definitely will look at storing a single ID as an option. |
I am trying to do this exact thing, except I will be saving multiple user IDs....any new word on this? |
Any chance you could store just the user ID, its making it impossible to query posts based on this field?
The text was updated successfully, but these errors were encountered: