0

I want to make a simple thing: display wordpress posts on my website from sql code. I have done this, which is very bad:

use App\Http\Controllers\Controller;

class WordPressController extends Controller
{
    private $datatableName = "www1";

   /** 
    * [collectData description]
    * 
    * @param  int $numElements
    * @param  null $page
    * @return 
    */
    public function collectData($numElements=0, $page=NULL)
    {
        global $mysqli;
        $query = "SELECT post_author, post_date, post_title, 
                    post_content FROM `www1_posts` ORDER BY post_date";
        $result = $mysqli->query($query);

        while(($post_row = $result->fetch_assoc))
        {
            //
        }
    }

    /**
     * [setDatatableName description]
     * 
     * @param string $datatableName
     */
    public function setDatatableName(string $datatableName) 
    {
        $this->datatableName = $datatableName;
    }
}

What I want to is map table "www1_posts" from Wordpress with a Model (Post) and a view which I 'll call "book.layout.blade.php"

I don't how to write migration from an existing table. Should I write down all the fields ? Or can I grab the table structure from Migration or a Laravel framework class.

1
  • 1
    Hmm... It sounds like you want to merge the two systems in a way that might be problematic (if you ask me). If it was me, then I would look into WordPress REST API and keep the two systems seperate. Commented Nov 29, 2018 at 2:39

1 Answer 1

3

You might want to try Corcel

Corcel allows you to use WordPress as backend (admin panel) and retrieve its data using Eloquent, with any PHP project or even framework.

After configuring and setting up the database you can try something like this:

// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Post::find(31);
echo $post->post_title;

Corcel Readme has more examples.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.