google-code-export / wordpress-custom-content-type-manager

Automatically exported from code.google.com/p/wordpress-custom-content-type-manager
2 stars 1 forks source link

Using one field to reference another post always calls up post ID 1 if empty #488

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a custom field which is a relational field to a page.
I need to query that related page and reference it's custom field to pull in 
images.

I'm using this code:

$refPage = get_custom_field('refPage:get_post');
$images = CCTM::filter( $refPage['page_images'], 'get_post');

which, for the most part, is working.

However, if either of the fields are empty (ie, 'refPage', or the related 
post's 'page_images') then, rather than returning false as I would expect, it 
returns the contents of the first post in the database (eg, post ID 1 "Hello 
World" if it hasn't been deleted, or the first post it comes across if it has).

I have tried it with plugins disabled, but obviously not with a default theme!

*SYSTEM INFO* 
------------------------ 
Plugin Version: 0.9.7.2-pl
WordPress Version: 3.5.1
PHP Version: 5.3.21
MySQL Version: 5.0.96-log
Server OS: Linux
Language: 
------------------------ 
ACTIVE PLUGINS: 
 * BackWPup v.3.0.10 [https://marketpress.com/product/backwpup-pro/]
 * JF3 Maintenance Redirect v.1.2 [http://www.hooziewhats.com/wpjf3_maintenance_mode]
 * Preserved HTML Editor Markup v.1.5 [http://www.marcuspope.com/wordpress/]
 * SEO Ultimate v.7.6.1 [http://www.seodesignsolutions.com/wordpress-seo/]
------------------------ 
CURRENT THEME: Two By Two Theme

Original issue reported on code.google.com by pe...@twobytwo.co.uk on 8 May 2013 at 10:43

GoogleCodeExporter commented 9 years ago
The problem is WP and its over-reliance on global variables.   It usually 
defaults to the current page ID, which is not what's expected.

Original comment by ever...@fireproofsocks.com on 8 May 2013 at 1:25

GoogleCodeExporter commented 9 years ago
I've updated the get_post filter so it responds with a false on empty input.  
Committed revision 713101.  Will be in 0.9.7.4.

However, you should put some tests into your code, e.g. 

if ($refPage = get_custom_field('refPage:get_post')) {
    if ($refPage['page_images']) {
        $images = CCTM::filter( $refPage['page_images'], 'get_post');
    }
}

Some filters do in fact operate on a blank value, e.g. the "default" output 
filter or the "help" filter.

Original comment by ever...@fireproofsocks.com on 14 May 2013 at 8:12