0

I have a custom Wordpress query that is using an array of product IDs that exist in an ACF field(these IDs do not line up with the Wordpress post IDs). I would like for the order of the returned posts to match the order of the array that is specified in the meta_query value. Here is what my meta query looks like:

$product_ids = array('3624', '1298', '430', '5629', '765');

$meta_query = array(
    'relation' => 'AND',
    array(
        'key' => 'product_id',
        'value' => $product_ids,
        'compare' => 'IN'
    ),
);

So, for instance, in the returned results I want the first product to be product ID 3624, and the last product to be 765. Is there a way to order the query results so that the order of the returned posts matches the order of the array specified in the meta query? I tried leaving orderby blank, but that seems to default to sorting by post ID. I also tried setting orderby to none, but that didn't seem to work either.

1
  • out of interest, is there a reason you can't do this at the point you consume the data? i.e. get an unordered list and pick out the items you want from it in whatever order you want?
    – mozboz
    Jun 30, 2023 at 17:48

2 Answers 2

2

Use PHP to sort the results after the query (untested):

$product_ids = array( 3624, 1298, 430, 5629, 765 );

$query = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'product_id',
            'value'   => $product_ids,
            'compare' => 'IN',
        ),
    ),
) );

$posts_by_product_id = array();
$sorted_posts = array();

if ( $query->have_posts() ) {
    foreach ( $query->posts as $post ) {
        $product_id = get_post_meta( $post->ID, 'product_id', true );
        $posts_by_product_id[ $product_id ] = $post;
    }

    foreach ( $product_ids as $product_id ) {
        if ( ! array_key_exists( $product_id, $posts_by_product_id ) ) {
            continue;
        }

        $sorted_posts[] = $posts_by_product_id[ $product_id ];
    }

    $query->posts = $sorted_posts;
}

The $query->posts property will contain the found posts, ordered by product ID to match the order in $product_ids.

1
  • This works great, thanks! Jun 30, 2023 at 18:37
1

If the post type is product, and $product_ids is a list of IDs of products that you want to retrieve, then you can do this rather simply (untested):

$product_ids = array( 3624, 1298, 430, 5629, 765 );

$query = new WP_Query( array(
   'post_type' => 'product',
   'post__in'  => $product_ids,
   'orderby'   => 'post__in',
) );
3
  • Unfortunately the Product IDs are a custom field and are not in sync with the Wordpress post IDs, but this is definitely a good alternate solution if I can't find a way to sort by the product ID array index. Thanks for the suggestion! Jun 30, 2023 at 17:39
  • 1
    Ahh, that context is helpful. Will you please edit your question to make that clear?
    – Caleb
    Jun 30, 2023 at 17:40
  • Added additional context, thanks! Jun 30, 2023 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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