Juárez
Juárez

Reputation: 131

Merge multiple ACF variables in one array

I'm using ACF relationship fields. I'm displaying multiple hand selected posts blocks. There is a last posts block where I want to exclude all the hand selected ones before.

How do I make an array of all ACF's to select them to exclude them from the loop?

This is my code so far (not working, it works if I only use one variable)

<?php   
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');
$excluirtodo = array (
  $excluir,
  $excluir2,
  $excluir3,
  $excluir4,
  $excluir5
);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'posts_per_page' => 9,
  'paged'          => $paged,
  'post__not_in' => $excluirtodo
);

$the_query = new WP_Query( $args ); 
?>

EDIT [SOLVED]: as @disinfor pointed on the comments the solution was array_merge instead of array

Upvotes: 1

Views: 1993

Answers (2)

It seems to me a bad method to call 5 ACF fields and then combine them into an array at the time the page is built.
For me, this method is better:
1. Create a text AСF field - hide_excluir
2. Add filter in functions.php (When editing our page, all ACF fields with exceptions will be merged into an array and saved in the field that we created before).

add_filter('acf/save_post', 'excluir_post_filter', 20);
function excluir_post_filter($post_id) {

    if ( $post_id != 2 ) //Change to your page ID (or if you need use post type or page template, need modify)
        return;

    $excluir = get_field('bloque_6_posts');
    $excluir2 = get_field('bloque_2_posts');
    $excluir3 = get_field('post_destacado');
    $excluir4 = get_field('posts_destacados');
    $excluir5 = get_field('bloque_4_posts');

    $all_excluir = array_merge(
        $excluir,
        $excluir2,
        $excluir3,
        $excluir4,
        $excluir5
    );

    update_post_meta($post_id, 'hide_excluir', $all_excluir ); //Save array to our field

}

3. We use our field with get_post_meta

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'posts_per_page' => 9,
  'paged'          => $paged,
  'post__not_in' => get_post_meta( $post->ID, 'hide_excluir', true ) //Get our field with post array
);

$the_query = new WP_Query( $args );

For testing methods, you can install the Query Monitor plugin and see the difference in the number of queries to the database.

Upvotes: 0

disinfor
disinfor

Reputation: 11558

Adding my answer from the comments to help future visitors

You are currently passing an array of arrays to the post__not_in. You need to use array_merge to combine the arrays into a single array.

<?php   
$excluir = get_field('bloque_6_posts');
$excluir2 = get_field('bloque_2_posts');
$excluir3 = get_field('post_destacado');
$excluir4 = get_field('posts_destacados');
$excluir5 = get_field('bloque_4_posts');

// NEW CODE HERE
$excluirtodo = array_merge(
  $excluir,
  $excluir2,
  $excluir3,
  $excluir4,
  $excluir5
);
// END ARRAY_MERGE
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'posts_per_page' => 9,
  'paged'          => $paged,
  'post__not_in' => $excluirtodo
);

$the_query = new WP_Query( $args ); 
?>

Upvotes: 1

Related Questions