I am writing an script to get WordPress posts and user data outside WordPress files and folder, this script run by side of WordPress installation but will fetch data from WordPress as explained below. 1. Created a file showmydata.php in WordPress root folder. 2. place require( '../wp-load.php' ); in file to use WordPress function. 3. write class and functions in showmydata.php to show details to users

now i am facing problem to call WordPress function inside class. how can I use query_posts and others inside class.

following is my class structure.

require(  '../wp-load.php' );    
class MyData {
        public $data = "";
        public function __construct(){
        function get_posts(){
            query_posts(array('showposts'=> -1,'post_type'=>'coupon'));
            while(have_posts()): the_post();
    $api = new MyData;
  • 2
    Quick note: You really shouldn't use query_posts(). – Courtney Ivey Oct 17 '14 at 7:19
  • 1
    Also, what's the error that you're receiving? – Courtney Ivey Oct 17 '14 at 7:20
  • I need call other functions as well like for category and user function not just query_posts – Vikram Oct 17 '14 at 8:23
  • BTW it says Call to undefined function query_posts() – Vikram Oct 17 '14 at 8:29
  • Correct. But that would just be another reason ( not the important reason ) why query_posts() shouldn't be used. – Courtney Ivey Oct 17 '14 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.