0

I have displayed some specific post ID's in a custom template with wp_query. I am struggling to figure out how to make these in order. I want to show 1682 first and 1540 last. They do not show this way and 'orderby' => $successful_musics_array did not work for me. How can one achieve this?

      <?php

      $successful_musics_array = array('1682', '1684', '556', '1793', '558', '1788', '320', '1190', '1865', '1125', '1863', '1540');

      $query = new WP_Query( array(
      'post_type' => 'songs',
      'posts_per_page' => '15',
      'post__in'      => $successful_musics_array
      ) );

        if ( $query->have_posts() ) : ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>


      <?php get_template_part( 'content', 'music-success' ); ?>

        <?php endwhile; wp_reset_postdata(); ?>
        <!-- show pagination here -->
    <?php else : ?>
        <!-- show 404 error here -->
    <?php endif; ?>
3

For anyone struggling with this. 'orderby'=>'post__in' makes this work.

  <?php

  $successful_musics_array = array('1682', '1684', '556', '1793', '558', '1788', '320', '1190', '1865', '1125', '1863', '1540');

  $query = new WP_Query( array(
  'post_type' => 'songs',
  'posts_per_page' => '15',
  'post__in'      => $successful_musics_array,
  'orderby'=>'post__in'
  ) );

    if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>


  <?php get_template_part( 'content', 'music-success' ); ?>

    <?php endwhile; wp_reset_postdata(); ?>
    <!-- show pagination here -->
<?php else : ?>
    <!-- show 404 error here -->
<?php endif; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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