我想在发布帖子时运行sql查询。我只是不确定如何将其包装在函数中以及在单击发布时将函数粘贴到哪里以使其生效。
以下命令是我需要的命令,它在phpMyAdmin- SQL中成功运行,并且也正确反映在我的wordpress后端中。
INSERT INTO `databasename`.`wp_xxxxxx` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', 'xxxx1', 'xxxx2', 'xxxxx3', 'xxxx4'
);
但是,每次保存帖子时,如何在我的functions.php文件中使它在PHP中运行?
我尝试了以下操作,但是我确定它是正确的,因为之后我会刷新,并且不会创建表:
function do_my_stuff($post_ID) {
mysql_query("INSERT INTO `databasename`.`wp_xxxxx` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', 'xxxx1', 'xxxx2', 'xxxx3', 'xxxxx4'");
return $post_ID;
}
add_action('save_post','do_my_stuff');
发布帖子时。它实际上是需要一些细节的产品。
这是我希望用于sql查询的值。
id =帖子的postID号。
名称=职务
item_number =应该是自定义字段发布元值,即将
从名为" scode"的字段开始,此刻我通过以下方式在我的单页模板上回显它:
ID,'scode',true))echo
do_shortcode(get_post_meta($ post-> ID,'scode',$ single = true))); ?>
价格=价格,我也通过自定义字段手动输入
我目前通过以下方式呼应的称为"价格":
options_2,我也可以通过自定义字段值手动输入
称为"变化"。
可以这样做吗?
也许像这样:
INSERT INTO `databasename`.`wp_cart66_products` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', '<?php the_title(); ?>', '<?php if ( get_post_meta($post->ID, 'scode', true) ) echo do_shortcode(get_post_meta($post->ID, 'scode', $single = true)); ?>', '<?php $values = get_post_custom_values("price"); echo $values[0]; ?> ', '<?php $values = get_post_custom_values("variations"); echo $values[0]; ?> '
);
请注意,我对php或SQL并不是很了解。我花了数小时试图找到尽可能多的信息,以提出可靠的要求。
您已经很接近了,当您将post do保存到add_action引用时,正在运行您定义的函数,但是do_my_stuff()的内容不太正确。
建议使用$wpdb全局变量访问数据库,甚至引用表。 例如,如果您使用默认前缀wp_,则希望对wp_users表使用$wpdb->users。 如果它不是标准表,但仍使用前缀(例如插件),则可以使用{$wpdb->prefix}tablename。 您还应该使用$wpdb->prepare来帮助确保您正在生成有效的SQL(以及防止SQL注入)。
尝试这个:
function do_my_stuff($post_ID) {
global $wpdb; // the wordpress database object
// check if this is a revision, if so use the parent post_id
if ($parent_id = wp_is_post_revision( $post_id )) $post_ID = $parent_id;
// load the post
$post = get_post($post_ID);
// load postmeta values
$scode = get_post_meta($post_ID, 'scode', true);
$price = get_post_meta($post_ID, 'price', true);
$variations = get_post_meta($post_ID, 'variations', true);
// create sql, use %d for digits, %s for strings
$sql = $wpdb->prepare("INSERT INTO {$wpdb->prefix}xxxxx (id, name, item_number, price, options_2) VALUES (%d, %s, %s, %s)", $post_ID, $post->post_name, $scode, $price, $variations);
// run the query
$wpdb->query($sql);
}
// add custom function to run on post save
add_action('save_post', 'do_my_stuff');
您的代码看起来更安全/可靠。 当我将表添加到xxxxx并发布/更新帖子时,它不会反映在数据库中。 我有一个类似的代码似乎可以做到,但是我不确定它是否像您一样以友好安全的方式编写。 您可以快速浏览一下吗? 请把它粘贴在上面作为答案
关于代码的一些注意事项(不能在答案中添加注释):1:您不想在全局行中包含$post-编辑过的帖子可能有所不同。 我建议从$post=get_post($post_ID)访问它。 我还建议使用if ($parent_id = wp_is_post_revision( $post_id )) $post_ID = $parent_id;,因为"当前"帖子可能是修订版2:如果前缀更改,我会定义$tablename="{$wpdb->prefix}cart66_products";:对于get_post_meta(),请尝试$value!==false当所有其他方法都失败时,添加一些< x5>条目以查看正在执行的操作。
function do_my_stuff($post_ID) {
global $post,$wpdb;
$tablename="wp_cart66_products";
if($post->post_type =="post" && strlen( get_post_meta($post_ID, 'price', true))>0 ) {
$id = $wpdb->get_var("SELECT id FROM".$tablename." WHERE id=".$post_ID);
$data=array(
'id'=>$post_ID,
'item_number'=>get_post_meta($post->ID, 'scode', true),
'name'=>$post->post_title,
'price'=>get_post_meta($post->ID, 'price', true),
'options_2'=>get_post_meta($post->ID, 'variations', true)
);
$where = array("id" => $post_ID);
// Possible format values: %s as string; %d as decimal number; and %f as float.
$format=array( '%d', '%s', '%s', '%s', '%s');
$where_format = array( '%d' );
if($id>0){
// update
$wpdb->update( $tablename,$data, $where, $format, $where_format);
}else{
// insert
$wpdb->insert( $tablename,$data,$format);
}
}
return $post_ID;
}
add_action('publish_post', 'do_my_stuff');