3

Is there a bug or something that prevents post author’s functions not to work in customizer selective refresh callback?

It renders fine on initial page load, but author functions don't show after the refresh. Other functions seems to work fine.

Here is my demo callback function for selective refresh:

function refresh_callback() {
    echo get_author_meta('ID'); // isn’t rendered after refresh / setting change
    echo get_the_title(); // is rendered fine after refresh / setting change
}
1

The selective refresh request doesn't get the whole regular WordPress request context. That is why on initial load (regular request) it works, while on AJAX partial refresh it doesn't. In your case, the global $authordata is not set and get_the_author_meta() is relying on it when no user is explicitly provided.

You will need to do a little bit of work yourself:

function refresh_callback() {
    // First get the current post
    $current_post = get_post();
    // Test if we actually have a post content (we might be in an archive context)
    if ( ! empty( $current_post ) ) {
        echo get_the_author_meta('ID', $current_post->post_author );
        echo get_the_title( $current_post );
    }
}
  • does this actually work? why would a post exist at all at the context of a selective refresh request? AFAIK it is called from the costomizer which do not run the main query, therefor there is not going to be a post. If it does run the main query why does the author related data not set? – Mark Kaplun Jan 27 '18 at 18:38
  • The author data is separated from the post. You do get the main query, but not the adjacent auto-setup like author data. – Vlad Olaru Jan 28 '18 at 9:55
  • You are welcome. WordPress still needs to work on consistency here and there. – Vlad Olaru Jan 28 '18 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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