3

I have a post that contain images, let's say image with id 19, 12, 10. I attach image 19 first, 12 below the first and 10 as the last, and I need to retrieve them. I

$post_images = get_children( array(
    'post_parent' => $id,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
));

But i receive them sorted by id (10,12,19), how i get them with the order as i needed

2

If I'm understood, you want to get the attachment by the same order that you uploaded them. You can sort them by date, in this case:

$args = array(
    'orderby'          => 'date',
    'order'            => 'ASC',
    'post_type'        => 'attachment',
    'post_mime_type'   => 'image',
    'post_parent'      => $id,
    'post_status'      => 'inherit',
);
$posts = get_posts( $args ); 

This will sort your attachments by their date, which would most likely be what you are looking for.

1

The documentation for get_children isn't great (at the time of this answer), however get_children is simply a wrapper for get_posts(). This means that orderby and order are valid arguments for your query.

When you ask, "how i get them with the order as i needed", is the property you wish to order them by a valid orderby value? If so, your function call might look like this:

$post_images = get_children( array(
    'post_parent' => $id,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'orderby' => 'title'
    'order' => 'ASC',
));

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.