• Resolved wpmd19

    (@wpmd19)


    Hello, I’m developing a custom theme for my website. I’m running into challenges when trying to use the wp_insert_post() function to convert MySQL tables into a Custom Post Type named “data.”

    When I run this code on an active p[age template (eg the contact page), the code works as intended and the tables are converted into new posts under the data CPT. The challenge I’m having is that when I try to run this same code from a non page template file, none of the tables are converted into posts and I do not receive any errors feedback.

    I have isolated the $dbName variable and determined this portion is working as intended. Similarly, I have also been able to isolate the $tables array and determined the table names are being extracted as expected. Beyond these items, I have not been able to identify the exact issue that is preventing these tables from being inserted as “data” CPTs when run in a non page template file.

    Some preliminary research has me thinking that this problem may have something to do with differences in core WordPress functions and class that depend on which type of file is being loaded (ie page vs other). I have tried to fix the issue by explicitly loading in wp-load.php, but this does not seem to have fixed the issue.

    I greatly appreciate any assistance on this issue and will be happy to provide any additional information that may be needed to better uncover a solution.

    My full code for this file is located at: https://www.codebin.co/code/2563e3c (Note: codebin marked this file as HTML; it is PHP). I have also included below a snippet of the remaining code after the two isolated items mentioned above.

    <code>
    //Query table data by looping through $tables to get each table name
    for ($i = 0; $i < count($tables); $i++) {
    
        $sql = "SELECT * FROM $tables[$i]";
        $result = $con->query($sql);
    
        if ($result->num_rows > 0) {
    
            $json_string = json_encode($result->fetch_all());
    
            $newPost = array(
                'post_title' => $tables[$i],
                'post_content' => $json_string,
                'post_status' => 'publish',
                'post_type' => 'data',
                'comment_status' => 'closed'
            );
    
            //Insert the post into WP database
            $post_id = wp_insert_post($newPost);
        }
    }
    
    $con->close();
    </code>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    When this PHP file is called ?
    I think, you should wrap it in a PHP function and hook it in the appropriate hook (it depends if you need to run it in back or front office)
    https://developer.www.ads-software.com/plugins/hooks/

    Moderator bcworkz

    (@bcworkz)

    When this PHP file is called ?

    Or how? I suspect you attempt to call this file directly via HTTP request. If your code uses WP functions, you cannot do that. Including wp-load.php can work, but it’s not 100% portable, so it’s a poor solution and very frowned upon. If you don’t want to use your code from a page template but need to call WP functions, your options are limited.

    Make an Ajax request through /wp-admin/admin-ajax.php or a HTTP request through /wp-admin/admin-post.php. Either way, the request must include an “action” data value which is used to compose an action hook you’d use to execute your code. The difference is what happens to the response. With Ajax, the data is returned to the caller and it’s up to the calling script to do something with the response. With admin-post.php, it’s a new page request. What ever you output appears on the new page. The output is entirely un-themed. If you want it themed you need to build it into your code’s output.

    That said, including wp-load.php should have worked for you, even though it’s considered “wrong”. There’s something else amiss if this did not work. Unless that is identified and corrected, using proper methods will still fail as well.

    Thread Starter wpmd19

    (@wpmd19)

    @sebastienserre – you are suggesting that I init the function in functions.php with a hook and then call this from the file mentioned in my original post? Maybe with a cron job to regularly call the function and insert new tables as posts / check for updates to the existing MySQL data?

    At least in a function and hook it on a scheduled event https://developer.www.ads-software.com/reference/functions/wp_schedule_event/

    I always prefer adding features in a plugin than in the theme functions.php. In the website life, your feature can remain, but theme can change. Theme is for design, plugins for features.

    Thread Starter wpmd19

    (@wpmd19)

    Update: moving this to a plugin, wrapping it in a function and attaching it to an init hook resolved the errors.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_insert_post failing to create CPT’ is closed to new replies.