Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using Advanced Custom Fields plugin to assign some fields to posts with a certain category, but the fields does not appear before I publish the post.

I have made some links on the dashboard, so my client can just click on for example "new product", and then go to add new post with the category "products" already selected. But because my client has to publish this new post before it "belongs" to the category "products", the fields I have assigned to "products" via Advanced Custom Fields, are not there.

Has anyone got an idea how I can solve this?

share|improve this question
    
see this question /answer –  Milo Jan 8 '13 at 22:25
    
@Milo - thank you, this could be a soloution I might use for future project, but it is not exactly what I need for this one :) –  Cody Jan 8 '13 at 23:20
    
you can use the functionality built in to link to a specific selected category. after you select the category from the dropdown, look at the URL it sends you to. copy that URL and use that as your link target for a post with that category pre-selected –  Milo Jan 8 '13 at 23:22
    
@Milo - okay, thanks! Sounds cool. I will check it out again :) –  Cody Jan 8 '13 at 23:39
add comment

1 Answer 1

Another option, apart of the solution linked by @Milo, would be to remove the category rule from the field group, and control the visibility of this ACF group with jQuery.

So, assuming that the Meta Box with the ACF fields has the id #acf_79 and that you want it assigned to the category with ID 1, that is rendered as #in-category-1 in the category Meta Box, the following will do.
Use the browser inspector to get the correct ID's.

add_action( 'admin_footer-post.php', 'conditional_acf_metabox_wpse_78772' );
add_action( 'admin_footer-post-new.php', 'conditional_acf_metabox_wpse_78772' );

function conditional_acf_metabox_wpse_78772() 
{
    // Not our post type, do nothing. Adjust if using another post_type.
    global $current_screen;
    if ( 'post' != $current_screen->post_type ) 
        return; 

    ?>
    <script type="text/javascript">
    jQuery(document).ready( function($) 
    {    
        // For a smoother effect, hide the box with CSS and show it if "is(':checked')"
        if( ! $('#in-category-1').attr('checked')  )
            $('#acf_79').hide();

        // Watch the behavior of Category 1 checkbox
        $('#in-category-1').change( function () 
        {       
            // Show/Hide the ACF meta box   
            if( $(this).is(':checked') )
            {
                $('#acf_79').slideDown();
            }
            else
            {
                $('#acf_79').slideUp();
            }
        });
    });
    </script>
    <?php
}
share|improve this answer
    
Cool, thanks! It works perfect, if I add a new post and then clicks the category. But it does not work if I go to add new post via the link I have in my backend /wp-admin/post-new.php?defaultcatid=1 (the link is created via "Select Category to Post" plugin). Then I have to deselect the category and select it again (or publish he post), for the fields to be shown. Do you have another way I could link from the dashboard, to add new post, have the category I want pre selected, and have your code work, so that the fields are shown? :) –  Cody Jan 8 '13 at 23:19
    
Create another function for admin_footer-post-new.php and instead of doing the first checking/hiding, do $('#in-category-2').attr('checked',true); –  brasofilo Jan 8 '13 at 23:28
    
thank you for your reply - this way category 2 is always pre selected when I add a new post. I need my client to also be able to create a new post without this pre selected category. Is this possible? –  Cody Jan 8 '13 at 23:38
    
Well, you can use some query var in the URL to detect this, or give the client one hour of training to use the system ;) –  brasofilo Jan 8 '13 at 23:56
    
cool, thanks! (Clients are untrainable! :D) –  Cody Jan 9 '13 at 8:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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