Support

Account

Home Forums Front-end Issues Exclude post object IDs on repeater from loop

Solving

Exclude post object IDs on repeater from loop

  • edit
    Ok, all done. Just a little lost on PHP after sometime away. Replaced 'post__not_in' => array( $these ) for 'post__not_in' => $these and all works 🙂

    I tried this and didn’t actually works, since I need post__not_in to be like

    [post__not_in] => Array
                    (
                        [0] => 20
                        [1] => 76
                    )

    original post

    People, I’m building the new website for the studio I work for, and one of the features is to stick posts via a Repeater on homepage template.

    I know I could use the native sticky posts feature, but with Repeater it’s easier for the content managers to not get lost, and I can limit the number of posts that can be sticked.

    The point is: I set a Repeater field with only one field inside, a Post Object which returns only the ID. I’m trying to pass the IDs to the WP_Query I create after the fixed posts section but it’s not exactly working.

    Right now what I have is this

    <?php
    			if( $fixed ) {
    			    while ( have_rows('fixed_posts') ) : the_row();
    			     $array[] = get_sub_field('post'); 
    			    endwhile;
    			    $ids = implode(', ', $array);
    			};
    
    		$these = str_replace("Array,", "", $ids);
    		$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( $these ) ) ); ?> 

    But when I print the query I get this on post__not_in:

    [post__not_in] => Array
                    (
                        [0] =>   20, 76
                    )

    Somebody knows what I’m missing here?

    Also, if anyone could help me get the IDs properly, not using str_replace, it would be great!

  • 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, ðŸĪŠ

  • Gummi, so sorry for the long delay. I coudn’t even test your answer, the project pivoted and this features was discarded :/

    Thank you for your time 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Exclude post object IDs on repeater from loop’ is closed to new replies.