PHP code example of hanamura / wp-model

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

    

hanamura / wp-model example snippets


$post = new WPModel\Post($post_id);

// same properties of WP_Post are accessible
echo $post->post_date;
echo $post->post_title;
echo $post->post_mime_type;

$post = new WPModel\Post($post_id);

echo $post->permalink;

$post = new WPModel\Post($post_id);

$children = $post->children(array(
  'id' => array(10, 11, 12)
));
$attachments = $post->children(array(
  'post_type' => 'attachment'
));
$images = $post->children(array(
  'post_mime_type' => 'image'
));

$post = new WPModel\Post($post_id);

$custom_terms = $post->terms(array(
  'taxonomy' => 'custom_taxonomy'
));

$post = new WPModel\Post($post_id);

// get meta value
echo $post->meta->custom_field;

// set meta value
$post->meta->custom_field = 'hello';

$post = new WPModel\Post($post_id);

$thumbnail = $post->image(array(
  'size' => 'thumbnail'
));
echo $thumbnail->url;
echo $thumbnail->path;
echo $thumbnail->width;
echo $thumbnail->height;

$post = new WPModel\Post($post_id);

$images = $post->images;

// get WPModel\Image object
$images['full'];
$images['large'];
$images['medium'];
$images['thumbnail'];

$post = new WPModel\Post($post_id);

$related_posts = $post->group(array(
  'taxonomy' => 'custom_taxonomy',
  'options' => array('posts_per_page' => 5),
  'map' => function($post) {
    return CustomPostClass($post);
  },
));

$post = new WPModel\Post($post_id);

$prev_post = $post->neighbor(array(
  'post_type' => ['post', 'custom_post_type'],
  'direction' => 'prev',
));

$post = new WPModel\Post(0);

$post->exists; // => false

$post = new WPModel\Post($post_id);

$post->hasChild($child_post);

$post = new WPModel\Post($post_id);

$post->matchMimeType('image/jpeg');

// create by constructor
$term = new WPModel\Term($term_id, 'custom_taxonomy');

// get from WPModel\Post
$post = new WPModel\Post($post_id);
$terms = $post->terms(array(
  'taxonomy' => 'custom_taxonomy'
));

$child_terms = $term->children(array(
  'orderby' => 'count',
  'hide_empty' => false
));

// current user
$user = new WPModel\User();

// specify user id
$user = new WPModel\User($user_id);

// same properties of WP_User are accessible
echo $user->user_email;
echo $user->user_login;
echo $user->first_name;

$user = new WPModel\User();

// get meta value
echo $user->meta->rich_editing;

// set meta value
$user->meta->rich_editing = 'false';

$post = new WPModel\User();

$post->exists; // => true