programing

Word press oop wpdb in class

mailnote 2023. 3. 22. 21:50
반응형

Word press oop wpdb in class

OOP PHP는 처음이라서 커스텀클래스에서 $wpdb(WORDPRESS) 오브젝트를 사용하려고 하는데 방법을 모르겠어요.$wpdb로 기본 작업을 구현하려고 할 때마다 실패합니다.get_results(), ...와 같은 기본적인 것이 필요합니다.그럼 어떻게 하면 좋을까요?

global $wpdb;

$my_custom_table = $wpdb->prefix . "table_name";

$table_content = $wpdb->get_results("SELECT * FROM ".$my_custom_table);

이렇게 수업시간에 넣었어요.

Class MyClass{

     public function table_results(){
            //put in here
            return $this->table_content;
     }
}

그 수업은 내가 쉽게 부를 수 있도록 별도의 파일로 해야 한다.

이거 드셔보세요.

<?php
class MyClass {

private $wpdb;

public function __construct()
{
    global $wpdb;
    $this->wpdb = $wpdb;
}

public function table_results(){
    $my_custom_table = $this->wpdb->prefix . "table_name";

    $table_content = $this->wpdb->get_results("SELECT * FROM $my_custom_table");
    return $table_content;
}
}

언급URL : https://stackoverflow.com/questions/31096669/wordpress-oop-wpdb-in-class

반응형