• Resolved Randy A

    (@randy-a)


    When using the User multi-select, I noticed that the plugin saves the multiple ‘user’ custom fields as multiple records with the same metakey. I’ve tested the plugin up to version 2.2.0.

    Would there be a way to hook into the save and have it store it as a serialized array instead so as to make it meta query compatible?

    I know there are the cfs_pre_save and cfs_after_save hooks, but I not sure if either of those would be suitable to change the behaviour of how the plugin would save the values.

    https://ww.wp.xz.cn/plugins/custom-field-suite/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    No – it’s already meta query compatible. Serializing actually makes the search process more difficult.

    Storing the values separately also makes it much easier to do reverse lookups too, hence with get_reverse_related.

    Thread Starter Randy A

    (@randy-a)

    I understand what you mean, but unfortunately my experiments with Meta Queries have shown that since users are stored with the same key name, only the first record is considered in the meta query.

    It would make more sense (for my use case) for multiple users to be stored in a serialized array, so as to make it compatible with Meta Queries of type “IN” or “NOT IN”.

    Thread Starter Randy A

    (@randy-a)

    I was able to build a workaround that does not require modifying the plugin in any way.

    To those wondering what I’ve done, I created a ‘post_where’ filter that applies a custom where clause to the meta query like this (assuming there is a custom field of type User with keyname set to ‘prohibited_users’):

    function restrictToUser($where){
    	global $wpdb;
    	$user_id = get_current_user_id();
    	$where .= " AND '$user_id' NOT IN (SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = wp_posts.ID AND meta_key='prohibited_users')";
    	return $where;
    }
    add_filter('posts_where', 'restrictToUser');

    and then I ensure that the query args passed to WP_Query had 'suppress_filters' => false set.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Save User Metakey as Serialized Array’ is closed to new replies.