Wordpress: multiple queries

2

I'm doing queries to fetch posts according to different parameters of custom fields, but I'd like the final result of the 3 queries to be displayed together.

function query() {
        foreach ($values1 as $value1) {
            $arrays[0][] = array(
                'key' => 'my-value1',
                'value' => $value1,
                'compare' => 'LIKE'
            );
        }

        $arrays[1]['relation'] = 'OR';
        foreach ($values2 as $value2) {
            $arrays[0][] = array(
                'key' => 'my-value2',
                'value' => $value2,
                'compare' => 'LIKE'
            );
        }

    if(!is_null($sofwares)) {
        $arrays[2]['relation'] = 'OR';
        foreach ($values3 as $value3) {
            $arrays[0][] = array(
                'key' => 'my-value3',
                'value' => $value3,
                'compare' => 'LIKE'
            );
        }
    }
}

$args = array(
'category' => 'my-category',
'posts_per_page'  => 5
);
$args['meta_query'] = query(); //chamo a função para montar
$posts = get_posts($args);

How could I make all the results of this query come together, as if they were concatenated in a single group of posts to be shown in the loop. I tried to use arrays to do multiple queries, but it is not working as expected, which would put together all the posts found in the 3 queries.

    
asked by anonymous 06.09.2018 / 18:12

1 answer

1

You can use the function: array_merge () to merge the results found, for example:

        $args1 = array(
            'category' => 'my-category',
            'posts_per_page'  => 5
            'key' => 'my-value1',
            'value' => $value1,
            'compare' => 'LIKE'
        );
        $args2 = array(
            'category' => 'my-category',
            'posts_per_page'  => 5
            'key' => 'my-value2',
            'value' => $value2,
            'compare' => 'LIKE'
        );
        $posts_args1 = get_posts( $args1 );
        $posts_args2 = get_posts( $args2 );
        // todos os resultados será armazenada nessa variável, depois é so percorrer ;)  
        $mergedposts = array_merge( $posts_args1, $posts_args2 );
    
17.09.2018 / 20:08