最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp api - wordpress api make 2 custom post type with single request

programmeradmin1浏览0评论

i have messenger chatbot and if the user click Get Started i make singel api call with $sender_id as title to add new post to my CPT inquery and when i look to wordpress panel i see 2 of them with same title and when make another api call with $sender_id as title it gives me the old one i can't figure why it happend from beginning Thanks in advenced

i have messenger chatbot and if the user click Get Started i make singel api call with $sender_id as title to add new post to my CPT inquery and when i look to wordpress panel i see 2 of them with same title and when make another api call with $sender_id as title it gives me the old one i can't figure why it happend from beginning Thanks in advenced

Share Improve this question asked Jun 28, 2019 at 9:05 Mohammed AlamaMohammed Alama 1 3
  • 1 Hi, can you please share your code? – Milan Hirapra Commented Jun 28, 2019 at 9:09
  • this id my costum end point function get_inquiry_id($data){ $return = []; $title=$data['title']; $return = get_page_by_title($title, OBJECT, 'inquiry'); $return = $return->ID; return new WP_REST_Response($return, 200); } add_action('rest_api_init',function(){ register_rest_route('chatbot/v1','/inquiry/(?P<title>\d+)',array( 'methods'=>'GET', 'callback'=>'get_inquiry_id' )); }); – Mohammed Alama Commented Jun 28, 2019 at 9:46
  • and this is my code for if statment if Get Started clicked if ( $payload == 'firsthand' ) { $sender_id = ''.$sender_id.''; $data_array = array( "title"=>$sender_id ) ; callAPI('POST','http://alenke.test/wp-json/wp/v2/inquiry',json_encode($data_array)); – Mohammed Alama Commented Jun 28, 2019 at 9:46
Add a comment  | 

1 Answer 1

Reset to default 0

Please write function for get post by title

function get_page_by_post_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;

    $sql = $wpdb->prepare(
        "
        SELECT ID
        FROM $wpdb->posts
        WHERE post_title = %s
        AND post_type = %s 
        ORDER BY ID DESC
    ",
        $page_title,
        $post_type
    );

    $page = $wpdb->get_var( $sql );

    if ( $page ) {
        return get_post( $page, $output );
    }
}

And this is your inquire_id function like,

function get_inquiry_id($data){ 
    $return = []; 
    $title=$data['title']; 
    $return = get_page_by_post_title($title, OBJECT, 'inquiry'); 
    $return = $return->ID; 
    return new WP_REST_Response($return, 200); 
}
发布评论

评论列表(0)

  1. 暂无评论