• Hi,

    I’ve created a function to convert an image file AUTOMATICALLY into the the featured image on a post.

    (This image file is generated automatically when I upload a video to my site via ffdshow, but it is not added to the database)

    The conditional statement is below. Its supposed to check if the post has a video, AND featured image. If it does, then the function does nothing. However if there is NO featured image, then the rest of the function executes.

    Its not working.

    if(get_post_meta($post->ID, "_videoembed", true) AND get_post_meta($post->ID, "_wp_attached_file", true) != true)
    	{  code to execute if its true.}

    Please help

Viewing 6 replies - 1 through 6 (of 6 total)
  • Try:

    if( get_post_meta($post->ID, "_videoembed", true) && !get_post_meta($post->ID, "_wp_attached_file", true) )
    	{  code to execute if its true.}
    Thread Starter Anthony Acosta

    (@anthon1)

    Thanks.
    It worked.
    However it turns out that its not returning what I need.

    How do I check if there is a “featured image” attached to a post?

    I’ve tried

    global $post; global $wpdb;
    	$attached_file_check = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    
    if( get_post_meta($post->ID, "_videoembed", true) && !isset($attached_file_check) )
    	{
    //function to attach existing videothumbnail goes here
    }

    It keeps firing the function leading to thousands of entries in my database to same thumbnails.

    I am going to paste the entire function below, just in case you see other errors in my code.

    function postmeta_img_fromvideo() {
    	global $post; global $wpdb;
    	$attached_file_check = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    //if(get_post_meta($post->ID, "_videoembed", true) AND get_post_meta($post->ID, "_thumbnail_id", true) != true)
    if( get_post_meta($post->ID, "_videoembed", true) && !isset($attached_file_check) )
    	{
    		$videofilename = get_post_meta($post->ID, "_videoembed", true);
    		$vid_sub_ext = substr($videofilename, 0 , -4);
    
    		$thumb = $vid_sub_ext . '_1.jpg';
    		$thumbnail = str_replace('http://www.mydomainname.com/wp-content/', '', $thumb);
    
    		$wp_filetype = wp_check_filetype(basename($thumbnail), null );
    		$attachment = array(
    	     'post_mime_type' => $wp_filetype['type'],
    	     'post_title' => get_the_title(),
    	     'post_content' => 'Image for '.get_the_title(),
    	     'post_status' => 'inherit'
    		);
    
    		wp_insert_attachment($attachment, $thumbnail, $post->ID);
    
    		// Get Attachment ID for Post
    		$attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    
    		// Attached Image as Featured Thumbnail in Post
    		update_post_meta($post->ID, "_thumbnail_id", $attachment_id);
    
    	}
    }
    add_action('the_post','postmeta_img_fromvideo');

    Thanks again for your help. I’m trying to automate this stuff as much as possible.

    How do I check if there is a “featured image” attached to a post?

    I think that, to start with, you need to define how a featured image is attached to a post in the first place. There’s a set of post_thumbnail functions in the WordPress core that can be used to set, test and grab post thumbnails. But some themes use their own functions – possibly by using custom fields.

    So – what does your theme do to attach a featured image to a post?

    Thread Starter Anthony Acosta

    (@anthon1)

    I’m using the following function at the top of my functions.php

    add_theme_support( ‘post-thumbnails’ );

    I believe this activates the featured thumbnails and that its supposed to be supported by the new WP 3.0 (Oh, I’m using 3.0.1)

    Thread Starter Anthony Acosta

    (@anthon1)

    Did some additional reading (*doh) and maybe I can use the
    has_post_thumbnail
    to check this. However, I don’t know the syntax of integrating it into my existing function.

    ex:

    if has_post_thumbnail {

    //do nothing

    } else {

    //run my function to attach it

    }

    Maybe?

    I’m using the following function at the top of my functions.php

    add_theme_support( ‘post-thumbnails’ );

    In that case, it’s simple – use has_post_thumbnail().

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Trying to check if a condition is true, skip the function’ is closed to new replies.