0

I'm trying to create an array that looks like below.

$arr = array(
    'post_type'         => 'smart_contract',
    'post_status'       => 'publish',
    'author'            => $user_id,
    'meta_query'        => array(
            'relation' => 'OR',
                array(
                    'key'     => 'contract_type',
                    'value'   => 'erc721-nft',
                    'compare' => '=',
                ),
                array(
                    'key'     => 'contract_type',
                    'value'   => 'erc721-sbt',
                    'compare' => '=',
                ),
        ),
); 

But if you look at meta_query, I'm expecting more than two nested arrays (value => erc721-nft and value => erc721-sbt). So, I'm using a for-loop to add these in as below. I'm using array_merge but it overwrites the values instead of adding in more nested arrays.

foreach ( $contract_types as $contract_type ) {
    
    $or_array2 = array(
        'key'     => 'contract_type',
        'value'   => $contract_type,
        'compare' => '=',
    );
    
    $or_array = array_merge( $or_array, $or_array2 );

}

How can I append more nested arrays without any overwriting?

1

2 Answers 2

0

I'm not quite sure what you want to achieve, you should improve your question, meanwhile you could try with:

$or_array = array(); // Initialize the array

foreach ( $contract_types as $contract_type ) {
    
    $or_array2 = array(
        'key'     => 'contract_type',
        'value'   => $contract_type,
        'compare' => '=',
    );
    
    $or_array[] = $or_array2; // Add the array to the main array
}

// Add 'relation' key separately
$meta_query = array('relation' => 'OR') + $or_array;

// Now, use $meta_query for the main $arr
$arr = array(
    'post_type'   => 'smart_contract',
    'post_status' => 'publish',
    'author'      => $user_id,
    'meta_query'  => $meta_query,
);
Sign up to request clarification or add additional context in comments.

Comments

0

so you just want to add the contracts to the meta_query under relation => OR right ? You could do something like this:

$arr = array(
    'post_type'         => 'smart_contract',
    'post_status'       => 'publish',
    'author'            => '1234',
    'meta_query'        => array(
        'relation' => 'OR',
    ),
);

$contract_types = array(
    array(
        'key'     => 'contract_type',
        'value'   => 'erc721-nft',
        'compare' => '=',
    ),
    array(
        'key'     => 'contract_type',
        'value'   => 'erc721-sbt',
        'compare' => '=',
    )
);

foreach ($contract_types as $contract_type) {
    $arr['meta_query'][] = $contract_type; // add contract type to $arr array
}

^ above will result in:

Array
(
    [post_type] => smart_contract
    [post_status] => publish
    [author] => 1234
    [meta_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [key] => contract_type
                    [value] => erc721-nft
                    [compare] => =
                )

            [1] => Array
                (
                    [key] => contract_type
                    [value] => erc721-sbt
                    [compare] => =
                )

        )

)

1 Comment

Thanks but my $contract_types only contains [ "erc721-nft", "erc721-sbt" ]

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.