How can we force has_post_thumbnails()
to return false
?
Method #1 – disable theme support:
You can remove the post thumbnails support from the backend, with
function twentythirteen_child_setup()
{
remove_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'twentythirteen_child_setup', 11 );
but notice that this will not remove the featured images.
If the _thumbnail_id
post meta value is non-empty, then has_post_thumbnails()
will return true
. So we need something else, in the that case.
Method #2 – a script to modify the database:
If you want to un-attach the featured images, you could use for example (untested):
/**
* Un-attach all featured images:
*/
$pids = get_posts(
array(
'post_type' => array( 'post', 'page' ),
'meta_key' => '_thumbnail_id',
'fields' => 'ids',
'posts_per_page' => 10 // modify this to your needs
)
);
foreach( $pids as $pid )
{
// Delete:
// delete_post_meta( $pid, '_thumbnail_id' );
// Debug:
printf( ' pid: %d <br/>', $pid );
}
WARNING: Take a database backup before trying this code snippet.
Method #3 – hijack a filter:
If we go hunting for usable filters, then we are taken down the following path:
-
has_post_thumbnail()
callsget_post_thumbnail_id()
-
get_post_thumbnail_id()
callsget_post_meta()
-
get_post_meta()
callsget_metadata('post', ... )
-
get_metadata()
contains theget_{$meta_type}_metadata
filter
So the filter to use is the get_post_metadata
filter.
To hijack the returning value from the filter, $check
must fulfill:
! is_null( $check ) && false === (bool) $check
for has_post_thumbnail()
to return false
.
To better understand this criteria, we can make some tests:
var_dump( ! is_null( 0 ) && false === (bool) 0 );
var_dump( ! is_null( false ) && false === (bool) false );
var_dump( ! is_null( null ) && false === (bool) null );
which give the following output:
bool(true)
bool(true)
bool(false)
This means we can construct the following code snippet:
/**
* Force has_post_thumbnail() to return false.
*/
! is_admin() && add_filter( 'get_post_metadata',
function( $check, $object_id, $meta_key, $single )
{
// Hijack the meta data for featured images:
if ( '_thumbnail_id' === $meta_key )
{
$check = 0; // or false
}
return $check;
}
,10, 4 );
to force has_post_thumbnail()
to return false.
You can place this code snippet in the functions.php
file in your current theme directory or use it in your custom plugin.
ps: it looks like this has been solved few years ago, check for example this thread.