PHP code example of mrdulal / laravel-wp-connector
1. Go to this page and download the library: Download mrdulal/laravel-wp-connector library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use MrDulal\WpConnector\Facades\Wp;
// Get recent published posts with authors
$posts = Wp::posts()->published()->with('author')->latest()->take(5)->get();
// Get posts by category
$categoryPosts = Wp::postsByCategory('Technology');
// Get posts by tag
$tagPosts = Wp::postsByTag('Laravel');
// Get user count
$userCount = Wp::userCount();
// Get comment count
$commentCount = Wp::commentCount();
use MrDulal\WpConnector\Models\WpPost;
use MrDulal\WpConnector\Models\WpUser;
use MrDulal\WpConnector\Models\WpComment;
// Get a post with its author and comments
$post = WpPost::with(['author', 'comments'])->find(1);
// Get user's posts
$user = WpUser::find(1);
$userPosts = $user->posts()->published()->get();
// Get post comments
$post = WpPost::find(1);
$comments = $post->comments()->approved()->get();
// Get post terms (categories/tags)
$post = WpPost::find(1);
$categories = $post->terms()->categories()->get();
$tags = $post->terms()->tags()->get();
// Get post meta
$post = WpPost::find(1);
$featuredImage = $post->getMeta('_thumbnail_id');
$customField = $post->getMeta('custom_field', 'default_value');
// Set post meta
$post->setMeta('custom_field', 'custom_value');
// Get user meta
$user = WpUser::find(1);
$firstName = $user->getMeta('first_name');
$lastName = $user->getMeta('last_name');
use MrDulal\WpConnector\Facades\Wp;
// Get WordPress options
$siteName = Wp::option('blogname');
$siteDescription = Wp::option('blogdescription');
// Set WordPress options
Wp::setOption('custom_option', 'custom_value');
// Get posts with specific meta
$posts = Wp::posts()
->whereHas('meta', function ($query) {
$query->where('meta_key', 'featured')
->where('meta_value', '1');
})
->published()
->get();
// Get users with specific capabilities
$admins = Wp::users()
->whereHas('meta', function ($query) {
$query->where('meta_key', 'wp_capabilities')
->where('meta_value', 'like', '%administrator%');
})
->get();