Function Reference: geodir_set_wp_featured_image

Summary

Set first image as post’s featured image.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None
$plugin_prefix
(string) (required) Geodirectory plugin table prefix.

Default: None

Package

GeoDirectory

Parameters

$post_id
(int) (required) The post ID.

Default: None

Change Log

Since: 1.0.0

Source File

geodir_set_wp_featured_image() is located in geodirectory-functions/post_functions.php [Line: 2903]

Source Code

function geodir_set_wp_featured_image($post_id)
{

    global $wpdb, $plugin_prefix;
    $uploads = wp_upload_dir();
//	print_r($uploads ) ;
    $post_first_image = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT * FROM " . GEODIR_ATTACHMENT_TABLE . " WHERE post_id = %d and menu_order = 1  ", array($post_id)
        )
    );

    $old_attachment_name = '';
    $post_thumbnail_id = '';
    if (has_post_thumbnail($post_id)) {

        if (has_post_thumbnail($post_id)) {

            $post_thumbnail_id = get_post_thumbnail_id($post_id);

            $old_attachment_name = basename(get_attached_file($post_thumbnail_id));

        }
    }

    if (!empty($post_first_image)) {

        $post_type = get_post_type($post_id);

        $table_name = $plugin_prefix . $post_type . '_detail';

        $wpdb->query("UPDATE " . $table_name . " SET featured_image='" . $post_first_image[0]->file . "' WHERE post_id =" . $post_id);

        $new_attachment_name = basename($post_first_image[0]->file);

        if (geodir_strtolower($new_attachment_name) != geodir_strtolower($old_attachment_name)) {

            if (has_post_thumbnail($post_id) && $post_thumbnail_id != '' && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'delete')) {

                add_filter('wp_delete_file', 'geodir_only_supportable_attachments_remove');

                wp_delete_attachment($post_thumbnail_id);

            }
            $filename = $uploads['basedir'] . $post_first_image[0]->file;

            $attachment = array(
                'post_mime_type' => $post_first_image[0]->mime_type,
                'guid' => $uploads['baseurl'] . $post_first_image[0]->file,
                'post_parent' => $post_id,
                'post_title' => preg_replace('/\.[^.]+$/', '', $post_first_image[0]->title),
                'post_content' => ''
            );


            $id = wp_insert_attachment($attachment, $filename, $post_id);

            if (!is_wp_error($id)) {

                set_post_thumbnail($post_id, $id);

                require_once(ABSPATH . 'wp-admin/includes/image.php');
                wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));

            }

        }

    } else {
        //set_post_thumbnail($post_id,-1);

        if (has_post_thumbnail($post_id) && $post_thumbnail_id != '' && (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'delete'))
            wp_delete_attachment($post_thumbnail_id);

    }
}