来自表单数据的WP_INSERT_POST,使用WPDB-> GetResults

时间:2015-08-11 20:58:23

标签: php ajax wordpress forms custom-post-type

我有一系列自定义表,它们在与我的Wordpress相同的MySQL数据库中保存酿酒厂操作数据。我想使用WPDB-> GetResults,然后使用WP_INSERT_POST来获取表单数据并从某些酒厂表条目创建自定义帖子类型。我有一个HTML表单试图向PHP文件发送请求(下面的代码)。我注意到在打开网页时我甚至看不到PHP。这肯定有问题。调试表单提交时,我很好,直到我的$ .ajax({call,但我认为.php文件让我失望。

- 新手尽力

    <!DOCTYPE html>
<html>
<body>

<p>Goofball</p>
<?php
require('header.php');
require('mydomain/test/wp-includes/wp-db.php')

if(isset($_POST['mashId'])){
    $mashId = $_REQUEST['mashId']; 
    new_mash_post($mashId);
 }

function new_mash_post($mashId) {
    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    $mashes = $wpdb->get_results( 
    "
    SELECT * 
    FROM mash a
    INNER JOIN mashbill b ON a.mashId = b.mashId
    INNER JOIN mash_ferm_junc c ON a.mashId = c.mashId
    WHERE a.mashId = $mashId
    "
    );

    $oldmashid = 0; 
    foreach($mashes as $mash){

        if($oldmashid != 0){
            $oldmashid = $mash->mashId;
            $slug = $mash->mashId;
            $title = $mash->mashId;
            $author_id = 1;

            // If the page doesn't already exist, then create it
            if( null == get_page_by_title( $title ) ) {

                // Set the post ID so that we know the post was created successfully
                $post_id = wp_insert_post(
                array(
                'comment_status'    =>  'closed',   
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_name'     =>  $slug,
                'post_title'        =>  $title,
                'post_status'       =>  'publish',
                'post_type'     =>  'your_bottle'
                )
                );

                $json_result = array( 'success' => true, 'post_id' => $post_id);
                                echo json_encode( $json_result );

            // Otherwise, we'll stop
            } else {

                // Arbitrarily use -2 to indicate that the page with the title already exists
                    $post_id = -2;
                    echo json_encode("failed");

            } // end if


        }   //end if mashId is new
    } //end for loop over selection results


} // end programmatically_create_post
?>

</body>
</html>

0 个答案:

没有答案