1

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

6
  • Are all your posts from the blog or do they include custom post types? Commented Aug 27, 2018 at 18:29
  • What are the values of $excluir, $excluir2, $excluir3, etc? Commented Aug 27, 2018 at 18:51
  • 1
    Juarez, try this $excluirtodo = array_merge( $excluir, $excluir2, $excluir3 ) You are passing an array of arrays to the post__not_in..that won't work. With array_merge() you combine your arrays into one. Commented Aug 27, 2018 at 19:29
  • 1
    That's along the lines of what I was thinking, but wanted to see what the actual variables are Commented Aug 27, 2018 at 19:31
  • 1
    @disinfor thanks so much, array_merge did the trick. And yes, all of them were post type. Commented Aug 28, 2018 at 13:10

2 Answers 2

1

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 ); 
?>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.