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

    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 😉

    Cheers, 🤪