如何在Wordpress数据库中插入图像路径,在wp admin中显示产品图像而不是前端?

时间:2014-07-05 19:22:15

标签: wordpress

$insert = $wpdb->insert( $prefix."posts", array("post_title" => $posTitle,"post_content" => $postContent,"post_status" => "publish","post_type" =>"product"));

// select products ID
$select = $wpdb->get_results("SELECT ID AS productsId FROM ".$prefix."posts WHERE post_title='".$posTitle."'");
//echo $select[0]->productsId; die();
// insert Image in pots and product attribute meta posts table
$insertImg = $wpdb->insert($prefix."posts",array("post_status" =>"inherit","post_type" => "attachment","guid" => $postGuid,"post_parent" => $select[0]->productsId ));

我需要一个小的,我需要插入的表格和列在wordpress admin中显示产品图片,我在wordpress中通过查询插入产品而不是通过管理员插入产品。

1 个答案:

答案 0 :(得分:0)

wordpress featrured图像实际上是一个名为_thumbnail_id的元字段,因此它位于wp_postmeta

示例:

meta_id post_id meta_key meta_value
874 3767 _thumbnail_id 3841

在上面的示例中,meta_value实际上是附件的ID ..

您在查询中所做的实际上只是插入附件

$insertImg = $wpdb->insert($prefix."posts",array("post_status" =>"inherit","post_type" => "attachment","guid" => $postGuid,"post_parent" => $select[0]->productsId ));

现在,您需要添加相关的post_meta

,使该附件成为精选图片

现在,老实说,我对直接SQL查询(仅仅是基础知识)并不是很擅长,但是如果你在WP上运行整个过程 - 那么它有不同的方法来实现它。

1 - set_post_thumbnail( $newpostid, $attach_id );其中的参数非常明显,

2- update_post_meta($post_id, '_thumbnail_id', $attach_id); - 也是自我解释..

两者实际上都会这样做..

现在,就像我之前说过的那样 - 我已经完成了很多次这样的过程,但不是直接查询..

通常我只使用wp内置函数,例如:

$newpost = array( 'post_title' => $fullname, 
                        'post_content' =>'',
                        'post_status' => 'pending',
                        'post_category' => array(5),
);

    // Create the post
    $newpostid = wp_insert_post($newpost);

// $filename should be the path to the file ( upload directory for example ) 
            $filename = $dest_path;

            // Check the type of tile. We'll use this as the 'post_mime_type'.
            $filetype = wp_check_filetype( basename( $filename ), null );

            // Get the path to the upload directory.
            $wp_upload_dir = wp_upload_dir();

            // Prepare an array of post data for the attachment.
            $attachment = array(
                    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                    'post_mime_type' => $filetype['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
            );

            // Insert the attachment.
            $attach_id = wp_insert_attachment( $attachment, $filename, $newpostid );

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            // And now make it a featured image..
            set_post_thumbnail( $newpostid, $attach_id );

我希望这会让你走上正轨......