admin管理员组

文章数量:1279147

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; ?>

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; ?>
Share Improve this question asked Sep 25, 2021 at 11:22 lucrecelucrece 1248 bronze badges 2
  • 1 Can you show your query? – Howard E Commented Sep 25, 2021 at 11:47
  • I'm using the default wordpress query for the category.php file – lucrece Commented Sep 28, 2021 at 19:29
Add a comment  | 

1 Answer 1

Reset to default 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!

本文标签: Stores category posts in an array