I have developed a custom wordpress panel with a custom front-end recently. In single post pages there is a slider. I display the result of get_attached_media( 'image' ) function as the images of slider.

I told editor that upload only the images you would like to see in slider to post from edit post page, upload other images from the media link on the side menu. The problem is that, if the user uploads an image from media screen, then inserts it to content of the post, the image happens to be attached to that post since it is not attached to any post.

How can I prevent this? Or is there a way to show images which are uploaded from edit post page?

2 Answers 2


I´m using this code to load all images I uploaded via edit post page:

function get_gallery_images(){
    global $wpdb,$post;
        $ids = "";
        $counter = 0;
        $number_of_posts = 12;
        $args = array(
        'post_type' => 'attachment',
        'numberposts' => 12,
        'post_status' => null,
        'orderby' => 'rand',
        'post_parent' => $post->ID
        $attachments = get_posts($args);
        if ($attachments) {
            foreach ($attachments as $attachment) {

                if ($counter != 0) {
                    $ids .= ','.$attachment->ID;
                else {
                    $ids .= $attachment->ID;
        return $ids;

Also this line:

$attachment_ids = get_gallery_images();

And this inside content lines:

echo do_shortcode('[gallery columns="4"  include="'.$attachment_ids.'"]'); 

This last line is to show the images are gallery, if you need to show them in another format like slider or carousel this line should be changed according your needs.


The following code would provide a very basic interface. The idea is the following. On the edit page you get another meta box "Slider", where you can select which attached media shall be used for the slider. Here of course could be more UX work done. Right now, it only shows the already attached media. So the editor needs to add the media, save the post and can then select. There could be done some improvements.

Once, the editor defined some attached media to be for the slider, you won't use get_attached_media( 'image' ) anymore, but get_attached_slider(). Basically this function also outputs Post Objects, but only of the attached media, which shall be displayed in the slider.

Hope, this gives you a start for what you need.

    add_action( 'add_meta_boxes', 'slider_metabox' );
    function slider_metabox(){
                __( 'Slider Pictures', 'sl' ),
                'post'      //Use 'post' to display on Posts, 'page' to display on Pages

     * This function renders the Metabox
    function slider_metabox_render( $post ){
        $slider_attachments = get_post_meta( $post->ID, 'slider_attachments', true );
        $all_attachments = get_attached_media( 'image', $post->ID );
        if( empty( $slider_attachments ) )
            $slider_attachments = array();

         * We get our attached media and the media, which is already supposed
         * to be in the slider. We loop through all the attached media
         * output a checkbox and if the single media is
         * already for the slider, we check the checkbox

        <?php foreach( $all_attachments as $s ): ?>
        <li data-attachment="<?php echo $s->ID; ?>">
            <label for="attachment-<?php echo $s->ID; ?>">
                <?php echo wp_get_attachment_image( $s->ID, 'post-thumbnail' ); ?>
            <input id="attachment-<?php echo $s->ID; ?>" type="checkbox" <?php 
                if( in_array( $s->ID, $slider_attachments ) ): 
                    echo 'checked="checked" '; 
            ?>name="slider_attachments[]" value="<?php echo $s->ID; ?>" />
        <?php endforeach; ?>

     * Lets save our slider media
    add_action( 'save_post', 'slider_attachment_save' );
    function slider_attachment_save( $post_id ){
        if ( wp_is_post_revision( $post_id ) )

        if( isset( $_POST['slider_attachments'] ) )


     * get_attached_slider
     * Use this instead of get_attached_media() to retrieve only the slider images
    function get_attached_slider( $post_id = null ){
        if( $post_id == null )
            $post_id = get_the_ID();

        if( !is_numeric( $post_id ) ){
            $error = new WP_Error();
            $error->add( 'post-id', 'No valid post ID' );
            return $error;

        $attachment_ids = get_post_meta( $post_id, 'slider_attachments', true );
        if( empty( $attachment_ids ) || ! is_array( $attachment_ids ) )
            return false;

        $args = array(
            'post_type' => 'attachment',
            'post__in' => $attachment_ids,
            'post_status' => 'any',
            'posts_per_page' => count( $attachment_ids )
        $query = new WP_Query( $args );
        $attachments = $query->posts;
        return $attachments;

More information on how to realize this:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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