By AdamJones


2019-02-11 14:14:48 8 Comments

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 ?

1 comments

@AdamJones 2019-02-12 19:24:52

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 );
       }
    }

Related Questions

Sponsored Content

1 Answered Questions

0 Answered Questions

ACF Query meta_values don't work with 'offset'

2 Answered Questions

1 Answered Questions

[SOLVED] ACF query-pass field value

2 Answered Questions

[SOLVED] ACF: get_field() returning false

2 Answered Questions

1 Answered Questions

[SOLVED] How to use ACF repeater?

1 Answered Questions

[SOLVED] Query by radio button ACF

1 Answered Questions

[SOLVED] Top menu disapears in Category Pages when filtering using ACF

1 Answered Questions

[SOLVED] Sorting the portfolio by an ACF?

Sponsored Content