0

I have created some custom fields with Advanced Custom Fields and set them to appear in my custom post type named X (post type is equal to X).

But I only need them to display on a particular single page from this custom post type, not on all of them.

How would I do that?

  • So, really, this question doesn't specifically pertain to the ACF plugin, but rather to post custom meta data. You're essentially asking how to have certain custom meta data only appear on the edit page for a specific post. Question: how do/will you know which post you want to have display this custom meta data? – Chip Bennett May 21 '12 at 14:15
1

Couldn't you do this through ACF settings page? You can select Post and set it equal to the title.

Edit

Updated image for clarification

enter image description here

Edit 2

It turns out ACF does not query for custom type when display the title drop down. To include all post type, in /core/admin/meta_box_location.php, change

<div rel="post">
    <?php 

    $choices = array();
    foreach(get_posts(array('numberposts'=>'-1')) as $v)
    {
        $choices[$v->ID] = $v->post_title;
    }

    $this->create_field(array(
        'type'  =>  'select',
        'name'  =>  'location[rules]['.$k.'][value]',
        'value' =>  $rule['value'],
        'choices' => $choices,
    ));

    ?>
</div>

To

<div rel="post">
    <?php 

    $choices = array();
    foreach(get_posts(array('numberposts'=>'-1', 'post_type'=>'any')) as $v)
    {
        $choices[$v->ID] = $v->post_title;
    }

    $this->create_field(array(
        'type'  =>  'select',
        'name'  =>  'location[rules]['.$k.'][value]',
        'value' =>  $rule['value'],
        'choices' => $choices,
    ));

    ?>
</div>
  • That restricts it to post type, but the questioner wants to show it only on a single custom post type Post. Right now, there's no option for that. – mrwweb May 18 '12 at 19:09
  • The question is "But I only need them to display on a particular single page". So if he only needs a single page, then he can pick exactly that page and do not need to worry about the type. Its like saying I want the Honda Civic who's license plate is 123455. I can just say the car with license plate 123455. – ltfishie May 18 '12 at 21:25
  • @ltfishie, hang on, I meant a post, since this is a custom post type, My bad. However, it doesn't work either and there is apparently no way to only show these boxes I have created on just one post of a certain custom post type :/. – Chris May 21 '12 at 10:48
  • I updated the image. You can actually specify both post type and post name, and it worked for me. Match should be set to "All". Does that not work for you? Also, even if you don't select a post type and just specify the title, it should do what you needed. – ltfishie May 21 '12 at 14:09
  • I can set it up like: post_type is equal to "xxx" where xxx is the name of my custom post type. But then, for the second condition, I don't see the name of my posts from that xxx custom post type, it would have been piece of cake. – Chris May 21 '12 at 15:17
1

If you are referring to removing certain fields that you have added via PHP on post.php or post-new.php, when the post type meets a certain condition, then you can use the acf_remove_local_field( $key ) function - see https://www.advancedcustomfields.com/resources/register-fields-via-php/

I went with something along these lines:

<?php
// Add Local Field Groups and Local Fields
add_action('init', array($this, 'function_to_add_fields_to_all_post_types'), 999);

// Notice how the priority is higher on this set of add_action functions so it runs after the initial setup of the fields

// Remove Fields From Edit Post Screen
if ( $pagenow == 'post.php' && get_post_type($_GET['post']) != 'cpt' ) {
    add_action('init', array($this, 'remove_fields_from_cpt'), 9999);
}

// Remove Fields From New Post Screen
if ( $pagenow == 'post-new.php' ) {
    if(!isset($_GET['post_type']) || $_GET['post_type'] != 'cpt'){
        add_action('init', array($this, 'remove_fields_from_cpt'), 9999);
    }
}

function function_to_add_fields_to_all_post_types(){
    // Add Local Fields using acf_add_local_field_group() or acf_add_local_field()
}

function remove_fields_from_cpt(){
    // Note: The 'key' for your field is not the same as it's 'name'
    acf_remove_local_field( 'key');
}

I found that only adding the specific fields on specific admin screens broke some of the Ajax lookup functions, hence adding to all and then removing from specific admin screens.

There is probably a much more efficient way to do this, but this is working for me.

0

The following is a more efficient method:

This checks for both EDIT and ADD screen and uses the "acf_remove_local_field($key)" function to remove the selected field by the key.

add_action('init', 'remove_fields_from_cpt', 9999);
function remove_fields_from_cpt(){
    global $pagenow;
    // Remove ACF fields from add/edit post
    if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ), true )  
    && isset($_GET['post']) || isset($_GET['post_type']) 
    && in_array('YOUR_POST_TYPE', array(get_post_type( $_GET['post']), 
    get_post_type( $_GET['post_type']) )) ) {
        $key = 'FIELD_KEY';
        acf_remove_local_field($key);
    }   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.