$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>advanced custom fields - ACF: How to make get_field() ignore the main wp query?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

advanced custom fields - ACF: How to make get_field() ignore the main wp query?

matteradmin7PV0评论

I have a filter applied to the main WP query via a load action (pre_get_posts) for all the admin pages in my website. It simply limits the edit (listing page) to show records created only by the specific user currently logged in.

The problem is that this then stops ACF functions from working properly. I have a function called by another action that is loading via acf/init. The function queries a repeater field with get_field(). The value returned by get_field() is not the expected array but just integers. If I remove this filter code below the problem doesn’t occur.

// The filter code that shows only the current authors posts
add_action(‘pre_get_posts’, ‘query_set_only_author’);
function query_set_only_author( $wp_query ) {
    if( is_admin() && get_current_user_role()==”required_role” ) {
        if ( basename($_SERVER[‘PHP_SELF’])==”edit.php”){
            $wp_query->set( ‘author’, $current_user->ID );
       }
    }
}

So my question is, it seems get_field is having my general edit page filter applied to it as well, so how to I make it not use the filtered query ?

I have a filter applied to the main WP query via a load action (pre_get_posts) for all the admin pages in my website. It simply limits the edit (listing page) to show records created only by the specific user currently logged in.

The problem is that this then stops ACF functions from working properly. I have a function called by another action that is loading via acf/init. The function queries a repeater field with get_field(). The value returned by get_field() is not the expected array but just integers. If I remove this filter code below the problem doesn’t occur.

// The filter code that shows only the current authors posts
add_action(‘pre_get_posts’, ‘query_set_only_author’);
function query_set_only_author( $wp_query ) {
    if( is_admin() && get_current_user_role()==”required_role” ) {
        if ( basename($_SERVER[‘PHP_SELF’])==”edit.php”){
            $wp_query->set( ‘author’, $current_user->ID );
       }
    }
}

So my question is, it seems get_field is having my general edit page filter applied to it as well, so how to I make it not use the filtered query ?

Share Improve this question edited Feb 11, 2019 at 15:48 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Feb 11, 2019 at 14:14 AdamJonesAdamJones 3282 gold badges7 silver badges26 bronze badges 1
  • I don't know if ACF sets anything you can check when it's running a query, if it does then I'd use that in the pre_get_posts filter. Otherwise I would use a singleton class and set a flag variable when calling the function that uses get_field then check for that variable in the pre_get_posts filter – mrben522 Commented Feb 11, 2019 at 14:43
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved this by adding wp_query->is_main_query() to my clauses before I set the query update. Ie...

// The filter code that shows only the current authors posts
add_action(‘pre_get_posts’, ‘query_set_only_author’);
function query_set_only_author( $wp_query ) {
    if( is_admin() && get_current_user_role()==”required_role” && $wp_query->is_main_query() ) {
        if ( basename($_SERVER[‘PHP_SELF’])==”edit.php”){
            $wp_query->set( ‘author’, $current_user->ID );
       }
    }
Post a comment

comment list (0)

  1. No comments so far