Hi there, I have a custom home page and with some relationships field I build a “featured posts” so I can handpick this posts.
Then I have a “latest Articles” module but I need to exclude the handpicked posts before selected.
This is my code
<?php
$posts = get_field('selecciona_post_hero');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<section class="home-pannello vinili hero">
<a href="<?php the_permalink (); ?>">
<div class="sfondi">
<div class="row">
<div class="col-sm-4 hidden-xs">
</div>
<div class="col-md-offset-1 col-sm-7 img" style="background-image: url(<?php the_post_thumbnail_url(); ?>)">
</div>
</div>
</div>
<div class="container">
<div class="contenuto">
<div class="row">
<div class="col-xs-12 col-sm-5">
<span class="category"><?php $categories = get_the_category(); if ( ! empty( $categories ) ) { echo esc_html( $categories[0]->name );}?></span>
<h2 class="hero-title"><?php the_title (); ?></h2>
<div class="hero-excerpt"><?php echo excerpt(30) ?></div>
<button class="primary">Gotham Light</button>
</div>
</div>
</div>
</div>
</a>
</section>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<div class="container destacados-home">
<div class="row">
<?php
$posts = get_field('selecciona_posts_destacados_1');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink (); ?>">
<div <?php post_class('col-sm-6'); ?>>
<img src="<?php the_post_thumbnail_url(); ?>">
<div class="color"></div>
<div class="meta">
<div class="date"><?php the_time('j M, Y') ?></div>
<h3 class="title"><?php the_title (); ?></h3>
<div class="excerpt"><?php echo excerpt(15) ?></div>
<button class="secondary">ver más</button>
</div>
</div>
</a>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php
$posts = get_field('selecciona_posts_destacados_2');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink (); ?>">
<div <?php post_class('col-sm-6 quote'); ?>>
<div class="color-quote">
<?php the_field ('quote'); ?>
<button class="secondary">ver más</button>
</div>
</div>
</a>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php
$posts = get_field('selecciona_posts_destacados_3');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink (); ?>">
<div <?php post_class('col-sm-6 bg-imagen'); ?>>
<div class="content" style="background-image: url(<?php the_post_thumbnail_url(); ?>)">
<div class="col-sm-6">
<h3 class="title"><?php the_title (); ?></h3>
<div class="excerpt"><?php echo excerpt(8) ?></div>
<button class="third">leer más</button>
</div>
</div>
</div>
</a>
<?php endforeach; ?>
</div>
<hr>
</div>
<!-- MY LATEST ARTICLES MODULE-->
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<div <?php post_class('bposts bloque1 col-md-6'); ?>>
<div class="content">
<span class="entry-date"><?php echo get_the_date('j F, Y'); ?></span>
<br>
<h2><a class="titulo" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php echo the_title(); ?></a></h2>
<p class="excerpt"><?php echo excerpt(10) ?></p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
// this array_merge will fail if any of the fields do not have selections
// this is quick to show an example
// each field should be gotten separately
// checked and merged if it is not empty
$selected_posts = array_merge(
get_field('selecciona_post_hero', false, false),
get_field('selecciona_posts_destacados_1', false, false),
get_field('selecciona_posts_destacados_2', false, false),
get_field('selecciona_posts_destacados_3', false, false));
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged,
'post__not_in' => $selected_posts
);
The topic ‘Exclude Relationship Field Posts from Custom Query’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.