PHP code example of rumur / wp-eloquent-models

1. Go to this page and download the library: Download rumur/wp-eloquent-models 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/ */

    

rumur / wp-eloquent-models example snippets



use Rumur\WordPress\Eloquent\Model\Attachment;

// Getting an attachment 
$attachment = Attachment::find(2020);

// Available relationships
$attachment->meta;
$attachment->post;
$attachment->author;

// As a WordPress Entity
$attachment->toWordPressEntity(); // <- ?\WP_Post


use Rumur\WordPress\Eloquent\Model\Comment;

// Getting a comment
$comment = Comment::find(2020);

// Available relationships
$comment->meta;
$comment->post;

// As a WordPress Entity
$comment->toWordPressEntity(); // <- ?\WP_Comment


use Rumur\WordPress\Eloquent\Model\Post;

// Getting a post
$post = Post::find(2020);

// Available relationships
$post->meta;
$post->terms;
$post->author;
$post->comments;
$post->attachments;

// As a WordPress Entity
$post->toWordPressEntity(); // <- ?\WP_Post

// Taxonomy Scope
$posts = Post::limit(15)->taxonomy('post_tag')->get();

// Status Scope
$published = Post::limit(15)->status('publish')->get();

// Post Type Scope
$orders = Post::with(['author'])->limit(15)->type('order')->get();


use Rumur\WordPress\Eloquent\Model\Term;

// Getting a term with a specific id
$term = Term::with(['posts'])->find(2020);

// Available relationships
$term->meta;
$term->posts;

// As a WordPress Entity
$term->toWordPressEntity(); // <- ?\WP_Term

// Taxonomy Scope
$tags = Term::limit(15)->taxonomy('post_tag')->get();


use Rumur\WordPress\Eloquent\Model\User;

// Getting a user with a specific id
$user = User::find(2020);

// Available relationships
$user->meta;
$user->posts;
$user->comments;

// As a WordPress Entity
$user->toWordPressEntity(); // <- ?\WP_User


use Rumur\WordPress\Eloquent\Model\{Attachment, Comment, Post, Term, User};

$post = Post::find(2020);

$post->setMeta('progress_status', 88);
$featured_img_id = $post->getMeta('_thumbnail_id');

// The same approach can be applied for all other models.

$user = User::find(2020);

$networks = $user->getMeta('networks');

$user->setMeta('networks',  [
	'twitter' => 'https://twitter.com/username',
	'facebook' => 'https://facebook.com/username', 
	'instagram' => 'https://instagram.com/username',
]);

$attachment = Attachment::find(2020);
$meta = $attachment->getMeta('any_attachment_meta_key');

$comment = Comment::find(2020);
$meta = $comment->getMeta('any_comment_meta_key');

$term = Term::find(2020);
$meta = $term->getMeta('any_term_meta_key');

// Delete meta.
Post::find(2020)->deleteMeta('any_meta_key');
Term::find(2020)->deleteMeta('any_meta_key');
User::find(2020)->deleteMeta('any_meta_key');
Comment::find(2020)->deleteMeta('any_meta_key');
Attachment::find(2020)->deleteMeta('any_meta_key');



namespace App\Model;

use Rumur\WordPress\Eloquent\Model\Post;
use Rumur\WordPress\Eloquent\Scope\HasPostTypeScope;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Product extends Post
{
    // Adds a global `post_tpe` scope
    use HasPostTypeScope;

    /**
     * The `post_type` for a model.
     *
     * @var string
     */
    protected static $postType = 'cpt_product';

    public function stores(): HasManyThrough
    {
        return $this->terms()->where('taxonomy', 'store');
    }
}


namespace App\Model;

use Rumur\WordPress\Eloquent\Model\Post;
use Rumur\WordPress\Eloquent\Scope\HasPostTypeScope;

class Page extends Post
{
    // Adds a global `post_tpe` scope
    use HasPostTypeScope;
}