Home Forums General Issues pre_get_posts effecting get_field inside loop


pre_get_posts effecting get_field inside loop

  • I’ve found some code that makes sticky posts work on category archive pages. It works nicely but it has consequential effect on the value returned from a get_field request inside the loop.

    Inside the loop I have a get_field that should return a Post Object but its only returning the ID (it is the correct ID though) not an object. A standard ACF text field return the value without issue using get_field.

    I’ve checked that the pre_get_posts action is run only once for the category query and that is the case so I’m at a loss as to why this is occurring.

    Below is the pre_get_posts action function, I’ve looked at reseting/unsetting different things but no change in the outcome.

    Any thoughts would be greatly appreciated. I guess I will re-work my code so the the get_field is set to return the Post ID and alter the code inside my loop accordingly. Would like to get a handle on this issue though.

    function get_term_sticky_posts(){
        // First check if we are on a category page, if not, return false
        if ( !is_category() )
            return false;
        // Secondly, check if we have stickies, return false on failure
        $stickies = get_option( 'sticky_posts' );
        if ( !$stickies )
            return false;
        // OK, we have stickies and we are on a category page, continue to execute. Get current object (category) ID
        $current_object = get_queried_object_id();
        // Create the query to get category specific stickies, just get post ID's though
        $args = [
            'nopaging' => true,
            'post__in' => $stickies,
            'cat' => $current_object,
            'ignore_sticky_posts' => 1,
            'fields' => 'ids'
        $q = get_posts( $args );
        return $q;
    function category_page_sticky_posts ( $q ){
        if (    !is_admin() // IMPORTANT, make sure to target front end only
             && $q->is_main_query() // IMPORTANT, make sure we only target the main query
             && $q->is_category() // Only target category archives
        ) {
            // Check if our function to get term related stickies exists to avoid fatal errors
            if ( function_exists( 'get_term_sticky_posts' ) ) {
                // check if we have stickies
                $stickies = get_term_sticky_posts();
                if ( $stickies ) {
                    // Remove stickies from the main query to avoid duplicates
                    $q->set( 'post__not_in', $stickies );
                    // Check that we add stickies on the first page only, remove this check if you need stickies on all paged pages
                    if ( !$q->is_paged() ) {
                        // Add stickies via the the_posts filter
                        add_filter( 'the_posts', function ( $posts ) use ( $stickies )
                            $term_stickies = get_posts( ['post__in' => $stickies, 'nopaging' => true] );
                            $posts = array_merge( $term_stickies, $posts );
                            return $posts;
                        }, 10, 1 );
    add_action('pre_get_posts', 'category_page_sticky_posts');
  • I don’t see any reason why this pre_get_posts filter should be effecting a get_field() call for a post object field. Are you sure that it’s this filter that’s causing it? If you disable only this filter did the problem go away?

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

The topic ‘pre_get_posts effecting get_field inside loop’ is closed to new replies.