如何以编程方式将内容项添加到自定义帖子类型?

时间:2014-08-28 作者:user658182

我有一个自定义的post类型,我想通过编程将数据导入其中。我跟踪了these instructionsthese, 我可以很容易地为WP的典型页面和帖子做到这一点。

但是,我的自定义帖子类型具有自定义字段,例如经度和纬度。如何计算这些名称(数组键)在post数组中应该如何引用?

$defaults = array(
            \'comment_status\' => \'closed\',
            \'ping_status\' => \'closed\',
            \'post_author\' => $author_id,
            \'post_name\' => $slug,
            \'post_title\' => $title,
            \'post_status\' => \'publish\',
            \'post_type\' => \'custom-post-type\',
       longitude => $my_long,   // <- what\'s the key here?
       latitude  => $my_lat     // <- what\'s the key here?
            );

$images = array_of_images; // <- how do these get added?
我需要向大约40个额外字段添加数据。

此外,我的每一项内容都有图像附件,我还想通过编程方式添加这些附件(无论是从URL还是从本地硬盘,无论哪种方法最好)。

作为回答,我正在寻找一个非常简单但功能完整的脚本(或至少是完全完整的伪代码),它将考虑到我需要采取的所有编程步骤,以便将帖子及其相关图像附件放入数据库。结果应该与我填写所有字段、上传媒体并在Wordpress中点击“发布”的结果相同。

4 个回复
最合适的回答,由SO网友:RRikesh 整理而成

您应该首先运行wp_insert_post() 将返回帖子ID。然后使用该帖子ID添加自定义字段。使用add_post_meta() 添加自定义字段。

$post_id = wp_insert_post( $args );

add_post_meta( $post_id, \'longitude\', $my_long );
add_post_meta( $post_id, \'latitude\', $my_lat );
有关图像附件,您可以参考以下问题:How to set featured image to custom post from outside programmatically

SO网友:jacobwarduk

完全没有经过测试,但我只是把它放在一起:

$post_data = array (
    \'comment_status\'    => \'closed\',
    \'ping_status\'       => \'closed\',
    \'post_author\'       => $author_id,
    \'post_name\'         => $slug,
    \'post_title\'        => $title,
    \'post_status\'       => \'publish\',
    \'post_type\'         => \'custom-post-type\', 
);

$post_ID = wp_insert_post( $post_data );

if ( ! is_wp_error( $post_ID ) ) {

    $post_meta = get_post_meta( $post_ID );

    if ( $post_meta ) {

        foreach ( $post_meta as $key => $value ) {

            if ( preg_match(\'/(\\.jpg|\\.png)$/\', $value ) {

                $file = file_get_contents( $value );

                $tmpfname = tempnam( \'/tmp\',  \'img\' );

                $handle = fopen( $tmpfname );

                fwrite( $handle, $file );

                if ( getimagesize( $tmpfname ) ) {

                    $image_url  = $value;
                    $upload_dir = wp_upload_dir();
                    $image_data = file_get_contents( $image_url );

                    $filename   = end( explode( \'/\', $image_url ) );
                    $fileName   = end( explode( \'/\', $filename ) );

                    $uploadDir  = end( explode( \'uploads\', $imageUrl ) );
                    $uploadDir  = str_replace( $fileName, \'\', $uploadDir );

                    $filename   = $fileName;

                    if ( wp_mkdir_p( $upload_dir[\'path\'] ) ) {

                        $file = $upload_dir[\'basedir\'] . $uploadDir . $filename;

                    } else {

                        $file = $upload_dir[\'basedir\'] . $uploadDir . $filename;
                    }

                    file_put_contents( $file, $image_data );

                    $wp_filetype = wp_check_filetype( $filename, null );

                    $attachment = array (
                        \'post_mime_type\'    => $wp_filetype[\'type\'], 
                        \'post_title\'        => sanitize_file_name( $filename ), 
                        \'post_content\'      => \'\', 
                        \'post_status\'       => \'inherit\',
                    );

                    $attach_id = wp_insert_attachment( $attachment, $file, $post_ID );

                    // Include image.php
                    require_once(ABSPATH . \'wp-admin/includes/image.php\');

                    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

                    wp_update_attachment_metadata( $attach_id, $attach_data );

                }

                fclose( $handle );

            } else {

                add_post_meta( $post_ID, $key, $value );

            }

        }

    }

}
代码应该是自文档化的,但是如果您有任何问题或者它不起作用,请随时询问。

SO网友:Liz Eipe C

以编程方式插入和更新post。http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

 if( $mydb->num_rows >  0) {

    echo \'Post \'.$postCSVContent[0].\' exist\'.PHP_EOL;
    $template_file = get_post_meta( $postCSVContent[0], \'_wp_page_template\', true );
    echo \'Template File : \'.$template_file.PHP_EOL;

    $updatePost = array(   
        \'ID\' => $postCSVContent[0],
        \'post_title\'    => $postCSVContent[\'1\'],
        \'post_content\'  => $postCSVContent[\'2\'],
        \'post_type\' => \'doors\',
        \'post_status\'   => \'publish\',
        \'post_author\'   => 1
    );

    wp_update_post( $updatePost );

    update_post_meta( $postCSVContent[0], \'_wp_page_template\', $template_file );
    updateAcf( $postCSVContent , $postCSVContent[0] );

}
else
{
    echo \'Post \'.$postCSVContent[0].\' is not existing\'.PHP_EOL;        
    echo \'Post parent \'.$parentId.PHP_EOL;

    $newIds = wp_insert_post( array(
            \'post_title\' => $postCSVContent[\'1\'],
            \'post_content\' => $postCSVContent[\'2\'],
            \'post_type\' => \'doors\',
            \'post_status\' => \'publish\',        
            \'post_author\'   => 1,
            \'post_parent\' => $parentId
    ));

    updateAcf( $postCSVContent , $newIds );
请参阅更新acf的教程。http://www.pearlbells.co.uk/insert-update-acf-image-repeater-field-programmatically/

SO网友:Vajirali Asamadi
<?php
require_once \'wp-load.php\'; //path of wp-load.php

$name = \'my post\';
$content = \'dummy Content here\';
$featured_image = \'fullimage url.jpg\'; //pass here full image url

$post_data = array(
    \'post_title\'    => wp_strip_all_tags( $name ),
    \'post_content\'  => $content,
    \'post_status\'   => \'publish\',
    \'post_type\'     => \'post\',
    \'post_author\'   => 1,
    \'post_category\' => array(1,2),
    \'page_template\' => \'\'
);
$post_id = wp_insert_post( $post_data, $error_obj );

// for custom field
//add_post_meta( $post_id, \'post_date\', \'Dec 5, 2018\' ); // second parameter is your custom field name, 3rd parameter is value

generate_Featured_Image($featured_image, $post_id );

function generate_Featured_Image( $image_url, $post_id  ){
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir[\'path\']))
        $file = $upload_dir[\'path\'] . \'/\' . $filename;
    else
        $file = $upload_dir[\'basedir\'] . \'/\' . $filename;
    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        \'post_mime_type\' => $wp_filetype[\'type\'],
        \'post_title\' => sanitize_file_name($filename),
        \'post_content\' => \'\',
        \'post_status\' => \'inherit\'
    );
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    require_once(ABSPATH . \'wp-admin/includes/image.php\');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $post_id, $attach_id );
    echo \'post created...\';
}
?>
结束

相关推荐

使用jQuery.get时出现admin-ajax.php(已中止)错误

有一件事让我很困惑,那就是:当我通过Ajax加载javascript文件并观察正在发出的http请求时,我在Firebug中看到以下错误:admin Ajax。php(中止)请参见此图片:导致此错误的是jquery get file函数:jQuery.get(\'/assets/js/shop.js\', function(data) { eval(data); }); 如果我删除了上面的脚本,那么就不会再有错误了。有什么建议吗?编辑1:我已经明白了为什么会出现这些中止的错误。在商店里。js文