List custom post types in dropdown

Firstly declare the variable: (customcategory)

global $userdata;

$errors = array();

$title      = trim($_POST['wpuf_post_title']);
$customcategory      = trim($_POST['customcategory']);
$content    = trim($_POST['wpuf_post_content']);
$tags       = wpuf_clean_tags($_POST['wpuf_post_tags']);
$cat        = trim($_POST['cat']);

Secondly the array for adding the post:

if (!$errors) {
    $frontend_post = array(
            'post_title'    => $title,
            'post_content'  => $content,
            'post_status'   => $post_status,
            'post_author'   => $userdata->ID,
            'post_category'    =>    array($_POST['cat']),
            'post_type' => $customcategory,
            'tags_input'    => $tags
    );

    $post_id = wp_insert_post($frontend_post);

and finally create the dropdrown from the get_post_types function:

<?php 
          $args=array(
                     'public'   => true,
                     '_builtin' => false
                     ); 
                $output = 'names';
                $operator = 'and';
                $post_types=get_post_types($args,$output,$operator); 

          echo '<select name="customcategory">';
          foreach ($post_types  as $post_type ) {
          echo '<option value="'. $post_type.'">'. $post_type. '</option>';
}echo '</select>';
?>

Taken from WordPress Stack Exchange.

Revisions

There are no revisions for this post.

Tags: ,

No comments yet.

Leave a Reply