• Resolved everyeurocounts

    (@everyeurocounts)


    I am trying to add a dropdown list using a cus to my publish box

    I can add the box no problem using a example from someone else’s post but the list just doesn’t populate with the custom taxonomy. The custom taxonomy is created in a plugin within a class and i’ve tried using the below in and out of the class to see if it makes a difference!

    Anyone any ideas?

    add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
    function publish_in_frontpage($post)
    {
        $value = get_post_meta($post->ID, '_publish_in_frontpage', true);
        echo '<div class="misc-pub-section misc-pub-section-last">
             <span id="timestamp">'
             . '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
        .'</span></div>';
    	echo 'select type'; wp_dropdown_categories(array('taxonomy'=>'gb_deal', 'show_option_none'=>'select type'));
    }
    
    function save_postdata($postid)
    {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
        if ( !current_user_can( 'edit_page', $postid ) ) return false;
        if(empty($postid) || $_POST['post_type'] != 'article' ) return false;
    
        if($_POST['action'] == 'editpost'){
            delete_post_meta($postid, 'publish_in_frontpage');
        }
    
        add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    This code would not be in the class, but part of the plugin’s main file, or in your theme’s functions.php. I’m assuming save_postdata() is referenced in an add_action() call somewhere, otherwise the setting will not be saved. Also, it appears the meta tag name in the first get_post_meta() line should not have a leading underscore _ as none of the similar references have one.

    As for the empty categories, the cause would have to be how the taxonomy is defined in the class. It will not be defined automatically by just loading the plugin, a class object would need to be instantiated, and perhaps a method invoked to get the taxonomy registered. You need to review the class code to see just what’s required to get the taxonomy registered.

    Thread Starter everyeurocounts

    (@everyeurocounts)

    Hi Thanks very much for your reply!

    sorry, re-reading the above, I guess i didn’t explain very well… the code above works, it will add the check box and the drop down box to the “PUBLISH BOX”, but it won’t show the taxonomy using wp_dropdown_categories in this box.

    I have a large plugin with a number of classes and its one of these classes (group) that deal with creating this custom post type and registering the custom taxonomy to that post-type and declaring a const of GB_DEAL_TYPE (the example above has the wrong name) and the function wp_dropdown_categories will work in the custom meta boxes created within the same class just fine.

    I guess the problem may be calling the taxonomy in the function as it is to a object outside of the class but im unsure of how to
    get the custom taxonomy correctly?

    Moderator bcworkz

    (@bcworkz)

    Well, taxonomies are stored in the global $wp_taxonomies, regardless of where they were registered. If you did a var_dump() of that array, you should see the taxonomy in there. So the dropdown should populate if fed the correct arguments. I think the only problem is how you may have referenced the constant outside of the class. It needs to be in this form: Class_Name::GB_DEAL_TYPE

    Since the meta boxes work fine within the class, I’m unsure why you are working outside the class. Not that I really care why, it’s just that the taxonomy will only work for the object type it was registered for. To apply it to other object types, use register_taxonomy_for_object_type().

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘modify post submit box (add wp_dropdown_category with custom taxonomy)’ is closed to new replies.