Can't reset the secondary query by wp_reset_postdata()

In custom post meta I have executed a custom query to get custom meta but when I tried to return to the main $post query, its printing the secondary loop.Where is my custom query and I have reset it by wp_reset_postdata().But no result.

                            $custom_fields  = get_posts( array(    'post_type'      = 'custom_field',
                                'posts_per_page' = -1,
                                'post_status'    = 'publish',
                                'meta_key'       = 'associate',
                                'meta_value'     = 'form'
                            ) );

                                foreach ($custom_fields as $post) {
                                    setup_postdata($post);
                                    var_dump(get_the_ID());
                                }
                                wp_reset_postdata();
                                wp_reset_query();

Topic wp-reset-postdata wp-query Wordpress

Category Web


wp_reset_query is meant to be used with a loop, because it resets the global $wp_query object. get_posts, on the other hand, just returns an array to be iterated with ie. foreach. setup_postdata does not populate the $wp_query object: https://codex.wordpress.org/Function_Reference/setup_postdata.

For a custom query, you would either use the WP_Query class, or you could reset your posts array using standard PHP functions like reset.

https://codex.wordpress.org/Function_Reference/wp_reset_query

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.