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.

Hmmm... maybe trickier than I hoped? If this is simply impossible then please do let me know :-) Question Refinement:How can I adapt the include section of this code to use category names instead of id's? Thanks

$dropdown = wp_dropdown_categories( array(
'include' => '58, 3',
    'name' => 'category_id[]',
    'hide_empty' => false,
    'echo' => false,
) );

I recently started using this code to force users of a multisite to select a category from a dropdown menu before creating a new post.

Can anyone help me restrict the category options to just a few specific categories eg. news / products? Even just not displaying sub-categories would be helpful. Thanks

UPDATE: I found adding this line I was able to restrict the choice to just 2 categories:

 'include' => '58, 3',

But how can I make this work with cat names / text instead of id numbers as it's for a multisite & I don't have control over cat id's when new sites are created????

CODE:

add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
$post_type = 'post';
if ( isset( $_REQUEST['post_type'] ) ) {
    $post_type = $_REQUEST['post_type'];
}

// Only do this for posts
if ( 'post' != $post_type ) {
    return;
}

if ( array_key_exists( 'category_id', $_REQUEST ) ) {
    add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
    return;
}

// Show intermediate screen
extract( $GLOBALS );
$post_type_object = get_post_type_object( $post_type );
$title = $post_type_object->labels->add_new_item;

include( ABSPATH . 'wp-admin/admin-header.php' );

$dropdown = wp_dropdown_categories( array(
    'name' => 'category_id[]',
    'hide_empty' => false,
    'echo' => false,
) );

$category_label = __( 'Category:' );
$continue_label = __( 'Continue' );
echo <<<HTML
<div class="wrap">
<h2>{$title}</h2>

<form method="get">
    <table class="form-table">
        <tbody>
            <tr valign="top">
                <th scope="row">{$category_label}</th>
                <td>{$dropdown}</td>
            </tr>
            <tr>
                <td></td>
                <th><input name="continue" type="submit" class="button-primary" value="{$continue_label}" /></th>
        </tbody>
    </table>
    <input type="hidden" name="post_type" value="{$post_type}" />
</form>
</div>
HTML;
include( ABSPATH . 'wp-admin/admin-footer.php' );
exit();
}

// This function will only be called when creating an empty post,
// via `get_default_post_to_edit()`, called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Well after thinking about this some more, I've realised that it was a bit of a dumb question to ask... My problem is actually being able to pre-define categories in themes for new subsites of my mulitiste.

I've got around this by using a premium plugin which allows me to offer blog templates / themes with a few predefined categories and some dummy content. This means that I already know the category id's when a new subsite is created.

Now new users can choose any of my themes, and most of them will have an e-shop option which will have the dropdown "product" category pre-installed etc.

Good grief, the lengths I'm going to to try and simplify the whole wp cms :-)

If anyone's interested I'll post a link to the full project here sometime soon (I hope).

share|improve this answer

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.