Fatal error: Uncaught Error: Call to undefined function get_posts() wordpress

Im having a problem on how to register my own ajax to wordpress I already read this and follow also this tutorial but seems an error occur

Error

Fatal error: Uncaught Error: Call to undefined function get_posts() in ...\copywriter-theme\orderer_creator_data.php:4 Stack trace: #0 {main} thrown in ...\copywriter-theme\orderer_creator_data.php on line 4

This is the my PHP

if(isset($_POST['type'])){
    $posts = get_posts(   //this is the line 4
            array(
                    'post_type'   => 'orderer',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'fields' => 'ids'
                )
            );
    $data = array();
    //loop over each post
    foreach($posts as $p){
        //get the meta you need form each post
        $data[] = array(
                "family_name" => get_post_meta($p,"family_name",true),
                "given_name" => get_post_meta($p,"given_name",true),
                "email" => get_post_meta($p,"e-mail_address",true),
                "reg_date" => $post->post_date,
                "last_mod" => $post->post_modified
        );
    }

    echo json_encode($data);
}

My Ajax

$(".toggle").on("change",function(){
        $.ajax({
            url: url,
            type: "POST",
            data: {type: $(".toggle option:selected").val()},
            success: function(result){
                $(".print").append(result);
            }
        });
    });

So far what I tried is to create a plugin and register their my ajax.

The plugin

add_action( 'init', 'my_script_enqueuer' );

function my_script_enqueuer() {
    wp_register_script( "ajax_script", get_stylesheet_directory_uri().'/js/copywriter.js', array('jquery'));
    wp_localize_script( 'ajax_script', 'Ajax_call', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'ajax_script' );

}

Upvotes: 2

Views: 2275

Answers (1)

hanish singla
hanish singla

Reputation: 792

You need to attach it with a action, so that default function can be loaded before your code executes.

function handle_ajax_request(){
    if(isset($_POST['type'])){
    $posts = get_posts(   //this is the line 4
        array(
                'post_type'   => 'orderer',
                'post_status' => 'publish',
                'posts_per_page' => -1,
                'fields' => 'ids'
            )
        );
    $data = array();
    //loop over each post
    foreach($posts as $p){
        //get the meta you need form each post
        $data[] = array(
            "family_name" => get_post_meta($p,"family_name",true),
            "given_name" => get_post_meta($p,"given_name",true),
            "email" => get_post_meta($p,"e-mail_address",true),
            "reg_date" => $post->post_date,
            "last_mod" => $post->post_modified
        );
    }

    echo json_encode($data);
    }


}

add_action( 'init', 'handle_ajax_request' );

Upvotes: 1

Related Questions