0

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

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.