Skip to main content
added 2 characters in body
Source Link
Caleb
  • 2.4k
  • 1
  • 14
  • 15

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$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.

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.

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.

Source Link
Caleb
  • 2.4k
  • 1
  • 14
  • 15

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.