Opened 8 years ago
Last modified 11 days ago
#45505 new defect (bug)
get_post_custom() doesn't pull values
| Reported by: | dryane | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Options, Meta APIs | Version: | 5.0 |
| Severity: | normal | Keywords: | has-patch needs-refresh |
| Cc: | Focuses: |
Description
When you are on a post or page and change the content and reload, so that the autosave post pops up at the top, the get_post_custom() values in custom metaboxes aren't being pulled in.
get_post_meta() values however still works in that scenario.
Attachments (1)
Change History (5)
#2
@
23 months ago
- Keywords has-patch needs-testing added; needs-patch removed
I've replaced the default get_post_custom() with get_post_meta() since get_post_custom() was not functioning as expected.
#3
@
9 months ago
- Keywords needs-refresh added; needs-testing removed
I attempted to apply 45505.patch against trunk, but it does not apply cleanly.
Because the patch cannot be applied, testing is currently not possible.
Removing needs-testing and adding needs-refresh so the patch can be updated against current trunk. A refreshed patch would be appreciated.
#4
@
11 days ago
I tested this on trunk (7.2-beta3) and couldn't reproduce it, in either the block editor or the classic editor.
Setup: a published post with a meta value, plus a newer autosave with different content so the "There is an autosave of this post that is more recent" notice shows. A metabox on that screen compares:
<?php add_meta_box( 't45505', 'T45505', function ( $post ) { var_dump( get_post_custom(), // no ID, uses global $post get_post_custom( $post->ID ), get_post_meta( $post->ID ) ); }, 'post' );
Result: all three return the same values with the autosave present, in both editors. The autosave doesn't affect the result.
That's expected, since get_post_custom() is just a wrapper:
<?php function get_post_custom( $post_id = 0 ) { $post_id = absint( $post_id ); if ( ! $post_id ) { $post_id = get_the_ID(); ... } return get_post_meta( $post_id ); }
The only way I could get them to differ is when no ID is passed and some other code on the edit screen changes the global $post, for example another metabox running a WP_Query loop without calling wp_reset_postdata(). In that case get_post_custom() reads meta from whichever post was last set up, while get_post_meta( $post->ID ) still returns the right values. That fits the original report (empty values that then get saved back), and it's a plugin/theme issue. The fix on that side is to pass the ID from the metabox callback: get_post_custom( $post->ID ).
About 45505.patch: it swaps internal callers of get_post_custom() for get_post_meta(), which doesn't change anything for this report since the wrapper already calls get_post_meta(). It also introduces a regression: get_post_custom_values( $key ) and get_enclosed() called without a post ID would then call get_post_meta( 0 ), which returns false instead of falling back to the current post. I confirmed this on trunk (get_post_custom_values( 'key' ) returns the value; the patched path returns false).
I'd recommend closing this as worksforme. If anyone can reproduce it on a clean install with no other plugins, steps would help reopen it.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
this of course is a big problem if you then save the post with empty values where filled values used to be