Wordpress:如何使用ACF和自定义帖子类型制作唯一字​​段 [英] Wordpress: How to make unique field with ACF and custom post type

查看:44
本文介绍了Wordpress:如何使用ACF和自定义帖子类型制作唯一字​​段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在研究一种自定义帖子类型,以与高级"自定义字段一起使用.我有这个小脚本,应该可以阻止输入重复的电子邮件.但是该脚本无法按我希望的那样工作.

So I'm working on a custom post type to work together with Advanced custom fields. And I have this little script that is supposed to block duplicate emails from being entered. But the script does not work as I want it to.

您在下面看到的代码用于我链接到ACF的自定义帖子类型页面.

The code that u see below is for the custom post type page that I've linked to ACF.

function init_members() {
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'menu_name'          => 'Members',
        'name_admin_bar'     => 'Member',
        'add_new'            => 'New member',
        'add_new_item'       => 'New member',
        'new_item'           => 'New member',
        'edit_item'          => 'Edit member',
        'all_items'          => 'All members',
        'search_items'       => 'Search member',
        'not_found'          => 'No members found',
        'not_found_in_trash' => 'No members found in trash'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'exclude_from_search' => true,
        'rewrite' => array('slug' => 'member'),
        'has_archive' => false,
        'supports' => array('title'),
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-groups'

    );
    register_post_type('members', $args);
}
add_action('init', 'init_members');


function add_member_columns ( $columns ) {
    unset($columns['date']);
    return array_merge ( $columns, array (
        'contactperson'   => __ ( 'Contactperson' ),
        'phone_number'   => __ ( 'Phonenumber' ),
        'email'   => __ ( 'Email' ),
    ) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );

function fill_member_columns ( $column, $post_id ) {
    switch ( $column ) {
        case 'contactperson':
            echo get_post_meta ( $post_id, 'contactperson', true );
            break;
        case 'phone_number':
            echo get_post_meta ( $post_id, 'phone_number', true );
            break;
        case 'email':
            echo get_post_meta ( $post_id, 'email', true );
            break;
    }
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );


因此,我要尝试过滤重复电子邮件的使用的脚本如下.我从中获得它的来源是此处

add_filter('acf/validate_value/name='.'email', 'acf_unique_value_field', 10, 4);

function acf_unique_value_field($valid, $value, $field, $input) {
    if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
        return $valid;
    }
    if (isset($_POST['post_ID'])) {
        $post_id = intval($_POST['post_ID']);
    } else {
        $post_id = intval($_POST['post_id']);
    }
    if (!$post_id) {
        return $valid;
    }
    $post_type = get_post_type($post_id);
    $field_name = $field['name'];
    $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish, draft, trash',
        'post__not_in' => array($post_id),
        'meta_query' => array(
            array(
                'key' => 'email',
                'value' => $value
            )
        )
    );
    $query = new WP_Query($args);
    if (count($query->posts)){
        return 'This Value is not Unique. Please enter a unique '.$field['label'];
    }
    return true;
}

我可能会监督一些显而易见的事情.但是我找不到我要监督的事情.

I probably oversee something pretty obvious. But I'm not able to find what I'm overseeing.

推荐答案

我认为问题可能在于您不是在查询所有现有帖子.如果您的每页帖子默认配置为5,而第6条帖子的邮件地址与更新后的邮件地址相同,则您的支票将找不到任何匹配的帖子.

I think the problem might be that you aren't querying all existing posts. If your posts per page default is configured to 5, and the 6th post turns out to have the same mail address as the updated one, your check will not find any matching posts.

尝试以下操作:在 acf_unique_value_field()中,像这样修复您的查询:

Try the following: in acf_unique_value_field(), fix your query like so:

    $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish, draft, trash',
        'post__not_in' => array($post_id),
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'email',
                'value' => $value
            )
        )
    );
    $query = new WP_Query($args);

'posts_per_page'=>-1,将查询无限数量的帖子.

'posts_per_page' => -1, will query an unlimited number of posts.

此外,我已经看到您正在使用 get_post_meta 来检索ACF字段值.我建议不要这样做.改用ACF的 get_field (必要时使用 update_field 进行更新).

Also, I've seen you're using get_post_meta to retrieve ACF field values. I advise against doing so. Use ACF's get_field instead (and update_field for updating if necessary).

这篇关于Wordpress:如何使用ACF和自定义帖子类型制作唯一字​​段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆