skip to Main Content

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?

2

Answers


  1. 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] => =
                    )
    
            )
    
    )
    
    Login or Signup to reply.
  2. 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,
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search