Support

Account

Home Forums General Issues How to exclude current page from acf relationship query Reply To: How to exclude current page from acf relationship query

  • The acf/fields/relationship/query/ only works in the admin when editing fields.

    If you want to do this on the front end there are couple of choices.

    The first it to compare the current post ID with the value in the loop and is probably the simplest and how I would go.

    
    // before the loop
    $current_post_id = $post->ID;
    
    // inside your loop before showing anything about the post
    if ($post->ID == $current_post_id) {
      // skip this post
      continue;
    }
    

    The other method would be to use your function as a pre_get_posts filter that is added before you get the value of the ACF field and removed after you get the value.

    
    // change to your function
    function exclude_id ($query) {
      $query->set('post__not_in', array(get_queried_object_id()));
    }
    
    
    //change to your template code
    add_action('pre_get_posts', 'exclude_id');
    $coin_pages = get_field('sb_sector_pages_pages', 'option');
    remove_filter('pre_get_posts', 'exclude_id');