Home › Forums › Front-end Issues › Exclude post object IDs on repeater from loop › Reply To: Exclude post object IDs on repeater from loop
G’day Mate,
1. Assuming your variable $fixed
is actually defined somewhere before the code you pasted and it’s true. (if it’s false, that block of condition won’t be run, and your array will be empty.
2. Depends on your php version, but i’m sure you need to declare the variable $array = []
before you can append.
3. If you want to use repeater, then you can utilize the built-in wp function wp_list_pluck
to avoid above mistake
https://developer.wordpress.org/reference/functions/wp_list_pluck/
So, you code can be simplify like:
// using elvis operator to ensure it returns an array if 'fixed_posts' has value
$exclude_ids = wp_list_pluck(get_field('fixed_posts')?: [], 'post');
$query = new WP_Query([
'post_type' => 'post',
'post__not_in' => $exclude_ids
]);
—-
P.S, for situation like this, I’d recommend you take a look at relationship field instead of using repeater 😉
https://www.advancedcustomfields.com/resources/relationship/
Cheers, 🤪
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.