Home › Forums › ACF PRO › How can I disable for selecting "non-public" posts from lists in relation field? › Reply To: How can I disable for selecting "non-public" posts from lists in relation field?
I tweaked your code a bit so it now also definitely prevents the user from even being able to select the post.
PHP:
add_action('acf/fields/relationship/result', 'add_extra_to_title' ,10 ,4);
function add_extra_to_title( $title, $post, $field, $post_id ) {
//prepare Jap translation for post_status
$poststatus_jp = array(
'draft' => '下書き',
'private' => '非公開',
'pending' => 'レビュー待ち',
'future' => '予約投稿',
);
$post_status = get_post_status( $post->ID );
//add flag for "cannotselect" item
if( $post_status != "publish" )
{
$title = '<span class="cannotselect">' . $title . '</span>';
}
//replace Eng to Jap.
if( $post_status != "publish" )
{
if(isset( $poststatus_jp[ $post_status ] ) ){
$post_status_jp = $poststatus_jp[ $post_status ];
}
$title = str_replace( "($post_status)" , "($post_status_jp)" , $title );
}
//if it's post, add category name to it's title.
$thisPost = get_post( $post->ID );
$thisPosttype = $thisPost->post_type;
$thisTerms = wp_get_post_terms( $post->ID, 'category' );
if( $thisPosttype == 'post' && !empty($thisTerms) )
{
$title .= " [" . $thisTerms[0]->name . "]";
}
return $title;
}
function my_acf_admin_enqueue_scripts()
{
wp_enqueue_script('customstuff', get_template_directory_uri() . '/customscript.js');
}
add_action('acf/input/admin_enqueue_scripts', 'my_acf_admin_enqueue_scripts');
JS:
jQuery(document).ready(function(){
watchAcfRelItem();
jQuery('.acf-bl').on('click', '.cannotselect', function(e){
e.preventDefault();
return false;
});
});
function watchAcfRelItem(){
jQuery('span.cannotselect').each(function(index, key){
jQuery(this).closest('li').addClass('cannotselect');
});
setTimeout( watchAcfRelItem, 1000 );
}
You can keep your CSS as it is 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.