投稿を公開するたびに SQL クエリを実行したいと思います。関数でラップする方法と、その関数をどこに貼り付けて、発行をクリックしたときに有効になるかがわかりません。私が必要とする次のコマンドは、phpMyAdmin-SQL で正常に実行され、wordpress バックエンドにも正しく反映されます。
INSERT INTO `databasename`.`wp_xxxxxx` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', 'xxxx1', 'xxxx2', 'xxxxx3', 'xxxx4'
);
しかし、毎回投稿を保存するときに、これを PHP の functions.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 = 投稿のタイトル
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についてはほとんど知らないことに注意してください。確実なリクエストを提供するために、可能な限り多くの情報を見つけようと何時間も費やしています。