Search code examples
wordpresscustom-post-typeshortcodewordpress-shortcode

Issue on custom post type shortcode ordering by ids


For a special purpose, i've to create a custom post type and display it like a carousel by indicating IDS of each custom post by it's ID on my shortcode.

The problem is that when i'm using it, the last id of custom shortcode is not displayed in on the correct place, sometimes in first, sometime in last but never in the correct place( for exemple I set 3527 ID on the 4 place, its shows this prodect on the last position.

I tried to show the array of the id and it seems that there isn't any problem with (array modification etc.) the Ids are in the correct place on Array

Here is the code :

//Carousel Shortcode Produits By ID
    function carousel_products_ids_shortcode( $atts ) {
        $c=extract(shortcode_atts( array(
            'ids'=>''
        ), $atts ));
        $args = array(
            'post_type' => 'pp_produit',
            'posts_per_page' => -1,
            'publish_status' => 'published',
            'order'=>'ASC'
        );
        if(!is_null($ids) && $ids!=""){
            $args['post__in']=explode(",",$ids);
            print_r($args['post__in']);
        }
        ob_start();
        $query = new WP_Query($args);
        
        if ( $query->have_posts() ) { ?>
        <div class="carousel-custom-products">
            <div class="owl-carousel owl-custom-products">
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <div class="single-produit">
                    <div id="produit-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <?php if(has_post_thumbnail()) {  ?>
                            <div class="img-container">
                                <a class="thumb-produit" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                            </div>
                        <?php }else{ ?>
                            <div class="img-container">
                                <a href="<?php the_permalink(); ?>" class="thumb-produit">
                                    <img src="<?php echo get_stylesheet_directory_uri()."/img/defaultpp.jpg" ?> ">
                                </a>
                            </div>    
                        <?php } ?>
                        <div class="produit-single-content">
                            <h3><a href="<?php the_permalink(); ?>" style="color:<?php the_field('couleur_du_titre_du_produit'); ?>" class="viga"><?php echo (strlen(get_the_title())>30)? substr(get_the_title(),0,30)."..." : substr(get_the_title(),0,30); ?></a></h3>
                            <?php the_excerpt(); ?>
                        </div>
                    </div>
                </div>
                <?php endwhile; ?>
            </div>
        </div>
        <?php $myvariable = ob_get_clean();
        return $myvariable;
        }   
    }
    add_shortcode( 'carouselproductsids', 'carousel_products_ids_shortcode' );

Here is the shortcode :

[carouselproductsids ids="3477,3479,3481,3527,3487,3483,3485,3489,3491,3496,3493"]

For example, the ID 3257 is on the last position when the carousel is generated !


Solution

  • the wp_query will fetch the posts from db according to "order" arg you passed so it will check if the post id is in the array or not but it will not preserve the order of array if you want to show the posts in specific order you can either pass some custom field (like meta key or someting) to posts to get them in the specific order or you can run loop on post_id array calling wp_query on each id