1. Go to this page and download the library: Download highliuk/wp-eloquent 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/ */
highliuk / wp-eloquent example snippets
HighLiuk\Eloquent\Database::instance();
use HighLiuk\Eloquent\Model\Post;
// Get the post with ID 1
$post = Post::find(1);
// Related data available
$post->author;
$post->comments;
$post->terms;
$post->tags;
$post->categories;
$post->meta;
Post::published()->get();
Post::status('draft')->get();
Post::type('page')->get();
use HighLiuk\Eloquent\Model\Comment;
// Get the comment with ID 12345
$comment = Comment::find(12345);
// Related data available
$comment->post;
$comment->author;
$comment->meta;
$post->terms()->where('taxonomy', 'country');
use HighLiuk\Eloquent\Model\User;
// All users
$users = User::get();
// Get the user with ID 123
$user = User::find(123);
$siteUrl = Option::get('siteurl');
Option::add('foo', 'bar'); // stored as a string
Option::add('baz', ['one' => 'two']); // the array will be serialized
$menu = Menu::slug('primary')->first();
foreach ($menu->items as $item) {
echo $item->instance()->title; // if it's a Post
echo $item->instance()->name; // if it's a Term
echo $item->instance()->link_text; // if it's a Custom Link
}
$items = Menu::slug('foo')->first()->items;
$parent = $items->first()->parent(); // Post, Page, CustomLink or Term (category)
class A extends \HighLiuk\Eloquent\Model\Post
{
protected static $aliases = [
'foo' => 'post_foo',
];
}
$a = A::find(1);
echo $a->foo;
echo $a->title; // retrieved from the Post model
// Display posts with 5 items per page
$posts = Post::published()->paginate(5);
foreach ($posts as $post) {
// ...
}
{{ $posts->links() }}
// Retrieves a meta (here 'link') from the Post model (we could have used another model like User)
$post = Post::find(31);
echo $post->meta->link; // OR
echo $post->fields->link;
echo $post->link; // OR
$post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // instance of PostMeta class
$trueOrFalse = $post->saveMeta('foo', 'baz'); // boolean
// Retrieves the first article with the meta "featured_article"
$post = Post::published()->hasMeta('featured_article')->first();
// Retrieves the first article with the meta "username" and having the value "highliuk"
$post = Post::published()->hasMeta('username', 'highliuk')->first();
$post = Post::hasMeta(['username' => 'highliuk'])->first();
$post = Post::hasMeta(['username' => 'highliuk', 'url' => 'highliuk.fr'])->first();
// Or just by providing the meta-data keys
$post = Post::hasMeta(['username', 'url'])->first();
// Will match: 'B Gosselet', 'B BOSSELET', and 'b gosselet'.
$post = Post::published()->hasMetaLike('author', 'B GOSSELET')->first();
// Using the % operator, the following results will be returned: 'N Leroy', 'N LEROY', 'n leroy', 'Nico Leroy' etc.
$post = Post::published()->hasMetaLike('author', 'N%Leroy')->first();
$post = Post::find(1);
// Retrieves an instance of HighLiuk\Eloquent\Model\Meta\ThumbnailMeta.
print_r($post->thumbnail);
// You must display the image instance to retrieve the url of the original image
echo $post->thumbnail;
$post = Post::find(1);
echo $post->acf->website_url; // returns the url provided in a field with the key website_url
// The method performing additional requests
echo $post->acf->author_username; // it's a field relative to User
// Without additional request
echo $post->acf->user('author_username');
// Other examples without requests
echo $post->acf->text('text_field_name');
echo $post->acf->boolean('boolean_field_name');
namespace App\Model;
use \HighLiuk\Eloquent\Model\User as BaseUser;
class User extends BaseUser {
public function orders() {
return $this->hasMany('\App\Model\User\Orders');
}
public function current() {
// fonctionnalité spécifique à l'utilisateur courant
}
public function favorites() {
return $this->hasMany('Favorites');
}
}
namespace App\Model;
user \HighLiuk\Eloquent\Model\Post as BasePost;
class Post extends BasePost {
public function countries() {
return $this->terms()->where('taxonomy', 'country');
}
}
Post::with(['categories', 'countries'])->find(1);
namespace App\Model;
class CustomPostType extends \HighLiuk\Eloquent\Model\Post {
protected $post_type = 'custom_post_type';
public static function getBySlug(string $slug): self
{
return self::where('post_name', $slug)->firstOrfail();
}
}
CustomPostType::with(['categories', 'countries'])->find(1);
// using the type() method
$videos = Post::type('video')->status('publish')->get();
// by defining its own class
class Video extends HighLiuk\Eloquent\Model\Post
{
protected $postType = 'video';
}
$videos = Video::status('publish')->get();
// Retrieving 3 elements of a custom content type and retrieving a meta-data (address)
$stores = Post::type('store')->status('publish')->take(3)->get();
foreach ($stores as $store) {
$storeAddress = $store->address; // option 1
$storeAddress = $store->meta->address; // option 2
$storeAddress = $store->fields->address; // option 3
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.