0

I'm trying to get my posts from my category.php file.

The loop works fine, it's showing my custom post types, but I'd like to store the posts from my category to reorder them in a certain way.

How do I do that ? Tried to use get_posts(), the it return the 3 first post of the 'post' post type.

    <?php
    if (have_posts()) : ?>
        <section class="playlist_row row">
            <div class="col-12 owl-container" id="<?= "";?>">
                <div class="owl-carousel owl-theme">
                <?php
                while (have_posts()) : the_post(); ?>
                        <?php get_template_part("template-parts/card/playlist", "card"); ?>
                <?php endwhile; ?>
                </div>
            </div>
        </section>
    <?php
    else: ?>
        <p>Aucun résultat.</p>


    <?php endif; ?>
2
  • 1
    Can you show your query?
    – Howard E
    Sep 25 at 11:47
  • I'm using the default wordpress query for the category.php file
    – lucrece
    Sep 28 at 19:29
1

As Howard E said, without knowing more about what and how you want to reorder it's difficult to give you useful advice. Here's a good breakdown of the options and, here are some places to start.

  1. If you want an array of posts to order your own way, you can use the native function get_posts() in order to create an array of posts based on selected arguments. More info here.
  2. If you want to modify the existing loop on your archive page and order youor posts with custom arguments, you can use pre_get_posts()
  3. If you want to write use existing WordPress arguments to order your posts, you can write a custom query using WP_Query and the ordering arguments it already supports.

Update

Since you want to sort your posts by a custom ACF field, here's how to do that with get_posts():

// get posts
$posts = get_posts(array(
    'post_type'         => 'event',
    'posts_per_page'    => -1,
    'meta_key'          => 'start_date',
    'orderby'           => 'meta_value',
    'order'             => 'DESC'
));

ACF has an entire page dedicated to sorting by custom field. You shouldn't need to put the posts back in an array in order to sort them.

Good luck!

2
  • I'm using the default query, just alter it by adding a custom post type in the query. For the reorder, it's gonna be I think an ACF reorder, but I need first to get my data in my template...
    – lucrece
    Sep 28 at 19:31
  • Great, answer updated to include ACF values too.
    – Pixelsmith
    Sep 29 at 23:48

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.