PHP code example of brandonwamboldt / wp-orm

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

    

brandonwamboldt / wp-orm example snippets


 

use WordPress\ORM\Model\Page;

$pages = Page::query()
	->limit(5)
	->where('post_status', 'publish')
	->sort_by('post_title')
	->order('ASC')
	->find();

use WordPress\ORM\Model\User;

$user = User::find_one_by('user_login', 'brandon');

echo $user->get_user_login();

print_r($user->to_array());

use WordPress\ORM\Model\Post;

$posts = Post::query()
	->limit(15)
	->offset(0)
	->where_all(['post_status' => 'publish', 'post_type' => 'post'])
	->where_like('post_title', '%Hello world%')
	->sort_by('post_title')
	->order('ASC')
	->find();

use WordPress\ORM\Model\Post;

$post = Post::find_one(1204);
$post->set_post_title('What an amazing post!');
$post->save();

$post = Post::find_one(1337);
$post->get_metadata('_edit_lock');
$post->update_metadata('_edit_lock', '');
$post->delete_metadata('_edit_lock');



namespace WordPress\ORM;

class Venue extends BaseModel
{
	protected $id;
	protected $venue_title;
	protected $description;
	protected $now_playing;
	protected $location;
	protected $avg_rating;

	public static function get_primary_key()
	{
		return 'id';
	}

	public static function get_table()
	{
		return 'wp_venues';
	}

	public static function get_searchable_fields()
	{
		return ['venue_title', 'description', 'now_playing'];
	}
}

add_filter('wporm_query', function($sql, $model_class) {
	if ($model_class == 'WordPress\ORM\Model\Page') {
		$sql = str_replace('wp_posts', 'wp2_posts', $sql);
	}

	return $sql;
}, 10, 2);

add_filter('wporm_count_query', function($sql, $model_class) {
	if ($model_class == 'WordPress\ORM\Model\Page') {
		$sql = str_replace('wp_posts', 'wp2_posts', $sql);
	}

	return $sql;
}, 10, 2);