首页 文章

在Wordpress中发布帖子并根据自定义字段元填充字段时运行SQL函数

提问于
浏览
2

每当我发布帖子时,我想运行一个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查询的VALUES .

  • id =帖子的postID号码 .

  • name =帖子 Headers

  • item_number =它应该是一个自定义字段后元值,来自名为'scode'的字段,此刻我在我的单页模板上通过以下方式回显它:
    ID,'scode',true))echo do_shortcode(get_post_meta($ post-> ID,'scode',$ single = true)); ?>

  • price =价格,我也通过名为'price'的自定义字段手动输入,我目前通过以下方式回复:

  • options_2,我也通过一个名为'variations'的自定义字段值手动输入 .

这一切都可以这样做吗?

也许是这样的:

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的了解不多 . 我花了好几个小时试图找到尽可能多的信息,以提供可靠的请求 .

2 回答

  • 3

    您非常接近,当您将post do保存到 add_action 引用时,您定义的函数正在运行,但 do_my_stuff() 的内容不太正确 .

    建议使用 $wpdb 全局变量来访问数据库,甚至引用表 . 例如,如果您使用的是 wp_ 的默认前缀,则需要将 $wpdb->users 用于 wp_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');
    
  • 0
    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');
    

相关问题