WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19708 closed defect (bug) (fixed)

get_post_custom returns no data when cache addition suspended

Reported by: leewillis77 Owned by: duck_
Milestone: 3.4 Priority: normal
Severity: normal Version: 3.3
Component: General Keywords: has-patch
Focuses: Cc:

Description

If cache addition is suspended via wp_suspend_cache_addition(true) then get_post_custom($foo) returns no data.

The cause is that in optimising for performance inside get_post_custom an assumption that cache values can be stored and subsequently retrieved. As a result, the return value is generated by calling wp_cache_get() which will fail to return anything since cache additions are suspended.

The values required are however already being generated and passed back into get_post_custom. The attached patch returns these without relying on a call to wp_cache_get.

Attachments (3)

19708.diff (1.2 KB) - added by leewillis77 2 years ago.
Proposed patch
19708-2.diff (1.2 KB) - added by leewillis77 2 years ago.
Revised patch attached - correctly deals with a false returned from update_postmeta_cache().
19708.3.diff (1.1 KB) - added by duck_ 2 years ago.

Download all attachments as: .zip

Change History (11)

leewillis772 years ago

Proposed patch

leewillis772 years ago

Revised patch attached - correctly deals with a false returned from update_postmeta_cache().

comment:1 leewillis772 years ago

  • Keywords has-patch added

comment:2 ryan2 years ago

  • Milestone changed from Awaiting Review to 3.4

This could also be a problem during imports.

comment:3 leewillis772 years ago

As with #19690, the use case that caused me to stumble on this is in plugins that wish to iterate over all (or a potentially large number of) posts. The specific use case that caused this is generating an XML feed of product information from e-Commerce stores for Google Merchant Centre.

In this context you want to iterate over all posts of a specific type, but there's no specific advantage in adding those posts (Or related information) to the cache - hence the use of wp_suspend_cache_addition(true).

Unfortunately as soon as cache additions are suspended, get_post_custom fails to return data for posts that do have custom info set.

duck_2 years ago

comment:4 duck_2 years ago

Nice catch.

I have attached 19708.3.diff which also simplifies get_post_custom() by essentially making it a wrapper for get_post_meta() with $meta_key set to ''.

comment:5 leewillis772 years ago

Looks good, and works for me ...

Thanks

comment:6 duck_2 years ago

  • Owner set to duck_
  • Resolution set to fixed
  • Status changed from new to closed

In [19906]:

Make get_post_custom() a wrapper of get_post_meta() so that it works when cache addition is suspended and to simplify it.

Props leewillis77 for the initial patch. Fixes #19708.

comment:7 scribu2 years ago

3 birds with one stone: #15483 #18702

comment:8 scribu2 years ago

Related: #19882

Note: See TracTickets for help on using tickets.