4

投稿を公開するたびに 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についてはほとんど知らないことに注意してください。確実なリクエストを提供するために、可能な限り多くの情報を見つけようと何時間も費やしています。

4

2 に答える 2

4

あなたはかなり近いです。投稿を参照に保存すると、定義した関数が実行されますadd_actionが、の内容はdo_my_stuff()まったく正しくありません。

$wpdbグローバル変数を使用してデータベースにアクセスし、テーブルを参照することをお勧めします。たとえば、デフォルトの接頭辞を使用している場合は、テーブルwp_に使用する必要があります。標準のテーブルではないが、プラグインなどのプレフィックスを使用している場合は、. また、有効な SQL を生成していることを確認する (および SQL インジェクションを防ぐ) ためにも使用する必要があります。$wpdb->userswp_users{$wpdb->prefix}tablename$wpdb->prepare

これを試して:

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');
于 2012-09-17T02:48:05.280 に答える
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');
于 2012-09-17T14:58:16.393 に答える