dongman5539 2015-08-11 20:58
浏览 40

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

I have a series of custom tables which hold distillery operations data on the same MySQL DB as my Wordpress. I would like to use WPDB->GetResults and then WP_INSERT_POST to take form data and create a custom post type from certain distillery table entries. I have an HTML form trying to send a request to a PHP file (code below). I notice that I cannot even see the PHP when opening the webpage. There must be something wrong with this. When debugging the form submission, I am good up until my $.ajax({ call, but I think the .php file is getting me down.

-Newbie doing his best

    <!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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 关于多单片机模块化的一些问题
    • ¥30 seata使用出现报错,其他服务找不到seata
    • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
    • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
    • ¥15 Google speech command 数据集获取
    • ¥15 vue3+element-plus页面崩溃
    • ¥15 像这种代码要怎么跑起来?
    • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
    • ¥15 pyqt5tools安装失败
    • ¥15 mmdetection