Skip to content
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

Open
ryanberry opened this issue Jul 18, 2012 · 6 comments
Open

Storing data as array #7

ryanberry opened this issue Jul 18, 2012 · 6 comments

Comments

@ryanberry
Copy link

Any chance you could store just the user ID, its making it impossible to query posts based on this field?

@lewismcarey
Copy link
Owner

Hi Ryan, can you give me an example. I've not had this feedback before. I'm
concerned the lates merge to the add on has affected things and with a real
case example I will roll it back for sure.

@ryanberry
Copy link
Author

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.

@lewismcarey
Copy link
Owner

Thats sounds like a really good addition.
I had never really thought of the scenario where you may want to query
posts by the meta.
I would need to see how it works for allowing multiple values as I am sure
this would still be saved as an array anyway.

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
in your functions.php that creates a new meta key and value when you save
the instructions post and query that...

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.

@ryanberry
Copy link
Author

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!

@lewismcarey
Copy link
Owner

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.

@nathanp
Copy link

nathanp commented Sep 29, 2012

I am trying to do this exact thing, except I will be saving multiple user IDs....any new word on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants