How to inject a class for all option elements from wp_get_archives()?

If there is none, we can write one:

function my_very_own_wp_dropdown_posts($args) {
    $args = shortcode_atts(array(
        'posts_per_page'   => 5,
        'offset'           => 0,
        'category'         => '',
        'category_name'    => '',
        'orderby'          => 'date',
        'order'            => 'DESC',
        'include'          => '',
        'exclude'          => '',
        'meta_key'         => '',
        'meta_value'       => '',
        'post_type'        => 'post',
        'post_mime_type'   => '',
        'post_parent'      => '',
        'author'       => '',
        'post_status'      => 'publish',
        'suppress_filters' => true,
        'echo' => true
    ), $args, 'wp-dropdown-posts');
    $echo = $args['echo'];
    unset($args['echo']);
    $all_posts = get_posts( $args );
    $output = false;
    if ($all_posts) :
        $output="
            <select name="archive-dropdown">
                <option value="">". esc_attr( __( 'Select Post' ) ).'</option>';
        foreach ($all_posts as $post) :
            setup_postdata($post->ID);
            $output .= "<option class="level-0" value="$post->ID">".$post->post_title."</option>";
        endforeach;
        wp_reset_postdata();
        $output .= '</select>';
    endif;
    $result = $output ?: esc_attr( __( "No posts found")).'.';
    if ($echo) echo $result; else return $result;
}
add_shortcode('wp-dropdown-posts', 'my_very_own_wp_dropdown_posts');

To get a dropdown with all your posts, just add [wp-dropdown-posts post_per_page="-1"] in any post or page (by default it only brings 5). If you want to use it in backend, you’ll have to wrap it in a do_shortcode():

do_shortcode("[wp-dropdown-posts post_per_page="-1"]");

Important: If you only want the HTML of the select returned, not echoed so you can echo it yourself inside a more complex function/class/plugin, replace echo with return in this line:

echo $output ?: __("No posts found").'.';

If you have more than 10 posts, you might want to combine this with a visual helper for selects (such as WP Chosen, a WordPress plugin wrapper for Chosen). While I recommend the library, I am not endorsing the WordPress plugin, nor have I extensively tested it. It’s just an example and it’s up to you to research and find a better one for the purpose.

If you have more than 50 posts, you might want to consider testing this plugin for your purpose, which has the advantage of dynamically populating your dropdown with search results based on what you type in. Typically, you want to avoid selects with more than 30 options. Again, I am not endorsing or supporting the plugin, nor have I tested it. It’s a(nother) WordPress plugin wrapper for Select2.

And, if you have more than 1000 posts, you might consider harnessing the power of Solr (or any other viable alternative). A quick search has revealed someone already made a Solr plugin for WordPress. Again, untested.


Updated the code based on the extra info from comments. Also added an echo paramter, true by default.