I have a custom loop for showing child pages of the current page, but I'd like to allow the WP user to enter page IDs to be excluded from the loop, using Advanced Custom Fields. My current loop is as follows. It almost works, except it only excludes the first page ID in the list:

 $exclude_ids = get_field('exclude_pages');
 $args = array (
 'post_type' => 'page', // custom post type
 'post_parent' => $post->ID,
 'orderby' => 'menu_order title', 
 'order' => 'ASC',
 'posts_per_page' => -1,
 'post__not_in' => array( $exclude_ids )

And the Advanced Custom Field is set as a text field, with no formatting. In that parent page, I have entered the page IDs in the ACF field as 2147,2145 (no quotes, no parenthesis). Interestingly, when I echo the variable, it shows the page IDs correctly, but it's just not working when it's part of the array...?

What am I doing wrong? Thank you!!! C

  • Don't forget to mark the answer as accepted please so future users can find help as well. This will also continue to show in the unanswered questions queue. – jdm2112 Nov 15 '16 at 17:24

Convert your string of IDs into a proper array:

$exclude_ids = get_field( 'exclude_pages' );
$exclude_array = explode( ",", $exclude_ids );

Then pass $exclude_array as the value for post__not_in.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.