Support

Account

Home Forums ACF PRO Retrieving option throws massive error

Solved

Retrieving option throws massive error

  • I am working on a site which shows profiles (a custom post type, submitted through ACF front-end form).
    No problems there.

    Now I have created a ‘dummy profile’ for which I have created an option, which is shown on an ACF menu page.

    The option is a simple true or false value option.
    $hide_dummy = get_field( 'sd_hide_dummy_profile', 'option' );

    As soon as this line of code is uncommented and I open an ACF group (to edit), I get this error: https://imgur.com/a/ITPcJ

    I have tried to change the true/false into a select option, but then the same thing happens.

    If I define it hardcoded as $hide_dummy = 1; it works without any problems.

    In the same ACF group (where this option is defined) I also have another true/false (to de-activate an NSFW filter for demo purposes) which works without any issues.

  • The JSON for this group can be found here.

  • Your filter is creating an infinite loop. When loading the field acf calls your filter. In your filter you call get_field(). This causes ACF to load the field and once again calls your filter.

    Please provide the code for the filter and how it is added. There are ways to prevent this from happening.

  • Thanks for the reply John. I can understand the infinite loop, but I don’t quite understand where or which filter this is ? AFAIK I have no filter for that field.

    I retrieve the value in a function and call that function within pre_get_posts and on other places.

    function sd_exclude_dummy() {
    
        // $hide_dummy    = 1; // 0 for all, 1 show for admins
        $hide_dummy    = get_field( 'sd_hide_dummy_profile', 'option' ); // creates issues
        $dummy_profile = get_field( 'sd_dummy_profile', 'option' );
    
        $exclude = array();
        if ( false != $dummy_profile ) {
            // a dummy post is 'set'
            if ( ! current_user_can( 'manage_options' ) ) {
                // non-admin, so always exclude it
                $exclude[] = $dummy_profile;
            } else {
                if ( $hide_dummy == 0 ) {
                    $exclude[] = $dummy_profile;
                }
            }
        }
    
        return $exclude;
    
    }
  • So this is called in a pre_get_posts filter? If it is then this could be the cause of the infinite loop unless you are making sure that your pre_get_posts filter is not running when ACF is doing a query to get the field.

    If this is the case

    1) WP does hook pre_get_posts
    2) Your filter runs and calls get_field()
    3) ACF does a query to get the field – Loopback to 1

  • Yes it is (located in a pre_get_posts filter), as shown below.

    function profile_search_form( $query ) {
        $results_per_page = get_option( 'posts_per_page' );
        $paged            = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $exclude          = sd_exclude_dummy();
    
        if ( ! is_admin() && $query->is_main_query() && isset( $_GET['ssd8'] ) && 1 == $_GET['ssd8'] ) {
    
            // output on search page
            if ( $query->is_search ) {
    
                $no_errors = false;
                include( get_template_directory() . '/inc/search/profile-search-query.php' );
    
                if ( true == $no_errors ) {
    
                    $query->set( 's', false );
                    $query->set( 'post_type', PostTypes::PROFILE );
                    $query->set( 'post__not_in', $exclude );
                    $query->set( 'posts_per_page', $results_per_page );
                    if ( isset( $_GET[ 'sp' ] ) && 'tour' == $_GET[ 'sp' ] ) {
                        add_filter( 'posts_where', 'sd_tour_cities_searches' );
                    }
                    $query->set( 'meta_query', $meta_query );
                    $query->set( 'tax_query', $tax_query );
                }
            }
        }
    }
    add_action( 'pre_get_posts', 'profile_search_form' );

    It exists in a few queries, since I need to hide this dummy post for regular visitors. It only needs to show in archives sometimes, when I test things.

    How can I easily solve this ?

    Would defining it as a constant be a simple solution ?

  • Put this $exclude = sd_exclude_dummy(); inside the if statement.

  • It appears to work, so thank you for that. But I don’t quite understand the difference yet…

  • Outside of the if your function is called every time the filter is run. The filter is run every time any query is made, including the queries ACF does to get the field. Inside it’s only run when $query->is_main_query() is true, which can only happen once per page load.

  • Gotcha, that last sentence is the key.

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

The topic ‘Retrieving option throws massive error’ is closed to new replies.