如何将数据插入post元表?我知道查询和wpdb是如何工作的,但我不知道如何将其插入表中。
我有这两个字段,我想插入ID和Title,其中Title应该成为我的自定义帖子类型公司中的新帖子,ID应该是它的内容。不幸的是,我不知道该怎么做。
编辑:我不确定它是否必须是post元表,但只要出现一个新的自定义post类型post。
提前感谢!
如何将数据插入post元表?我知道查询和wpdb是如何工作的,但我不知道如何将其插入表中。
我有这两个字段,我想插入ID和Title,其中Title应该成为我的自定义帖子类型公司中的新帖子,ID应该是它的内容。不幸的是,我不知道该怎么做。
编辑:我不确定它是否必须是post元表,但只要出现一个新的自定义post类型post。
提前感谢!
您不需要使用post_meta
这里,因为所有信息都可以在posts
.
要插入新帖子,请使用wp_insert_post( $post )
, 并将参数传递给$post
-大堆此函数可以返回WP_Error
-对象进行错误处理(如果第二个参数设置为true
, 错误时返回0(如果为false),并返回ID
插入立柱的。
请参阅的完整参数列表wp_insert_post()
在Codex.
$post = array(
\'post_content\' => $content, // The content you want to have set in the content
\'post_title\' => $title, // The title of your post.
\'post_status\' => \'publish\', // Whatever status you want to have
\'post_type\' => \'your_custom_post_type\' // the slug of your custom post type
);
$thisid = wp_insert_post( $post, true ); // insert the post and allow WP_Error object
if ( is_wp_error( $thisid ) ) {
// Error handling
} else {
// the rest of your code, inserting metadata
update_post_meta( $thisid, \'your_meta_key\', $your_meta_value );
}
您可以执行以下任何查询并检查是否成功,如果有效,请告诉我。
普拉文
$InsertQuery = "INSERT INTO post_meta VALUES (Enter Values Here)";
//Create a query named InsertQuery
$insert = $wpdb->query($InsertQuery);
//Execute InsertQuery
if($wpdb->insert_id){
echo \'Post Entered Successfully.\';
}else{
echo \'Unable to Insert Post.\';
}
//Check if the Query has run successfully
我正在制作一个页面模板,该模板应显示具有与页面标题匹配的元值的自定义帖子类型。元价值是通过Advanced Custom Field 单选按钮,但查询没有考虑meta,而是显示所有帖子。我查过了$name 存储正确的字符串,帖子的值正确program 钥匙以下是查询代码:$name = get_the_title(get_the_ID()); $args = array( \'post_type\' => \'project\', \'posts_per_pa