Friday, June 10, 2022
HomeWordPress Developmentwp question - Reset postdata not engaged on WP_Query in capabilities.php

wp question – Reset postdata not engaged on WP_Query in capabilities.php


I am working a WP_Query in my capabilities.php, so I can simply entry some ACF-fields in my code with out having to run that question on each web page.

Nonetheless, the loop adjustments the publish ID to the CPT I am calling with the WP_Query. That is after all identified conduct, so usually I’d simply add a wp_reset_postdata() to my question to proceed with the web page’s or publish’s unique ID.

Nonetheless, regardless of the place I reset the postdata in my question, it would not get reset. A brief, not so fairly, answer I’ve discovered is including wp_reset_postdata(); after I name the perform in my page-file. This fixes it, however is not very fairly and partially beats the aim of utilizing the perform.

This is my perform:

perform get_root_project_value( $meta_key ) {
    $args = array(
      'post_type'         => 'slug',
      'posts_per_page'    => -1,
      'meta_query'    => array(
        array(
            'key'       => 'project_is_root_project',
            'worth'     => '1',
        ),
      )
    );
    $undertaking = new WP_Query( $args );

    if ( $project->have_posts() ) {
        whereas ( $project->have_posts() ) { $project->the_post();
            
            if($meta_key) {
                $meta_value = get_field($meta_key, get_the_id());

                if($meta_value) {
                    return $meta_value;
                } else {
                    return false;
                }
            }
        }
        
        wp_reset_postdata();            
    } else {
        return false;
    }

}

And that is how I take advantage of it in my code:
$longitude = get_root_project_value('project_longitude');

This can set my present web page’s ID to the root_project ID, so to reset that (for the reason that wp_reset_postdata() within the perform would not try this), I add wp_reset_postdata(); simply after I set $longitude.

Is there a approach to reset the postdata within the perform, as a substitute on each single web page I take advantage of the perform?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments