1. Go to this page and download the library: Download williarin/wordpress-interop 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/ */
$connection = DriverManager::getConnection(['url' => 'mysql://user:pass@localhost:3306/wp_mywebsite?serverVersion=8.0']);
$objectNormalizer = new ObjectNormalizer(
new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())),
new CamelCaseToSnakeCaseNameConverter(),
null,
new ReflectionExtractor()
);
$serializer = new Serializer([
new DateTimeNormalizer(),
new ArrayDenormalizer(),
new SerializedArrayDenormalizer($objectNormalizer),
$objectNormalizer,
]);
$manager = new EntityManager($connection, $serializer);
// Fetch a post by ID
$post = $manager->getRepository(Post::class)->find(1);
// Fetch the latest published post
$post = $manager->getRepository(Post::class)
->findOneByPostStatus('publish', ['post_date' => 'DESC']);
// Fetch the latest published post which has 1 comment
$post = $manager->getRepository(Post::class)
->findOneBy(
['post_status' => 'publish', 'comment_count' => 1],
['post_date' => 'DESC'],
);
// Fetch the latest published post which has the most comments
$post = $manager->getRepository(Post::class)
->findOneByPostStatus(
'publish',
['comment_count' => 'DESC', 'post_date' => 'DESC'],
);
// Fetch all posts which have draft or private status
$posts = $manager->getRepository(Post::class)
->findByPostStatus(new Operand(['draft', 'private'], Operand::OPERATOR_IN));
// Fetch all posts
$posts = $manager->getRepository(Post::class)->findAll();
// Fetch all private posts
$posts = $manager->getRepository(Post::class)->findByPostStatus('private');
// Fetch all products whose titles match regexp
$products = $manager->getRepository(Product::class)
->findByPostTitle(new Operand('Hoodie.*Pocket|Zipper', Operand::OPERATOR_REGEXP));
// Fetch a product by its SKU
$product = $manager->getRepository(Product::class)->findOneBySku('woo-vneck-tee');
// Fetch the latest published product which is in stock
$product = $manager->getRepository(Product::class)
->findOneBy(
['stock_status' => 'instock', 'post_status' => 'publish'],
['post_date' => 'DESC'],
);
// Fetch all published products which are in stock
$products = $manager->getRepository(Product::class)
->findBy(
['stock_status' => 'instock', 'post_status' => 'publish'],
['post_date' => 'DESC'],
);
// Fetch all products whose sku match regexp
$products = $manager->getRepository(Product::class)
->findBySku(new Operand('hoodie.*logo|zipper', Operand::OPERATOR_REGEXP));
// Fetch Hoodies as well as products with at least 30 comments, all of which are in stock
$products = $manager->getRepository(Product::class)
->findBy([
new NestedCondition(NestedCondition::OPERATOR_OR, [
'post_title' => new Operand('Hoodie%', Operand::OPERATOR_LIKE),
'comment_count' => new Operand(30, Operand::OPERATOR_GREATER_THAN_OR_EQUAL),
]),
'stock_status' => 'instock',
]);
// Fetch two products by their SKU and two by their ID
$products = $manager->getRepository(Product::class)
->findBy([
new NestedCondition(NestedCondition::OPERATOR_OR, [
'sku' => new Operand(['woo-tshirt', 'woo-single'], Operand::OPERATOR_IN),
'id' => new Operand([19, 20], Operand::OPERATOR_IN),
]),
]);
// count($products) === 4
// Fetch the featured image of the post with ID "4"
$attachment = $manager->getRepository(Attachment::class)
->findOneBy([
new RelationshipCondition(4, '_thumbnail_id'),
]);
// Get featured images of posts 4, 13, 18 and 23 at once
$attachments = $manager->getRepository(Attachment::class)
->findBy([
new RelationshipCondition(
new Operand([4, 13, 18, 23], Operand::OPERATOR_IN),
'_thumbnail_id',
),
]);
// Same as above example but
// Fetch products in the category "Hoodies"
$products = $manager->getRepository(Product::class)
->findBy([
new TermRelationshipCondition([
'taxonomy' => 'product_cat',
'name' => 'Hoodies',
]),
]);
// Fetch a product's category and the category of its related product
$product = $manager->getRepository(Product::class)
->findOneBy([
new SelectColumns([
'id',
'main.name AS category',
'related.name AS related_category',
select_from_eav(
fieldName: 'related_product',
metaKey: 'related_product', // needed as it's not starting with an underscore
),
]),
new TermRelationshipCondition(
['taxonomy' => 'product_cat'],
termTableAlias: 'main',
),
new TermRelationshipCondition(
['taxonomy' => 'product_cat'],
joinConditionField: 'related_product',
termTableAlias: 'related',
),
'id' => 22,
]);
// $product->category === 'Hoodies'
// $product->relatedCategory === 'Accessories'
// Fetch products that have both 'featured' and 'accessories' terms
$products = $manager->getRepository(Product::class)
->findBy([
new TermRelationshipCondition([
'slug' => new Operand(['featured', 'accessories'], Operand::OPERATOR_IN_ALL),
]),
]);
// Fetch all terms of the product with SKU "super-forces-hoodie"
// belonging to all taxonomies except "product_tag", "product_type", "product_visibility".
$terms = $manager->getRepository(Term::class)
->findBy([
new SelectColumns(['taxonomy', 'name']),
new PostRelationshipCondition(Product::class, [
'post_status' => new Operand(['publish', 'private'], Operand::OPERATOR_IN),
'sku' => 'super-forces-hoodie',
]),
'taxonomy' => new Operand(
['product_tag', 'product_type', 'product_visibility'],
Operand::OPERATOR_NOT_IN,
),
]);
// Fetch only products title and SKU
$products = $manager->getRepository(Product::class)
->findBy([
new SelectColumns(['post_title', 'sku']),
'sku' => new Operand('hoodie.*logo|zipper', Operand::OPERATOR_REGEXP),
]);
// Product entities are filled with null values except $postTitle and $sku
$product = $manager->getRepository(Product::class)
->findOneBy([
new SelectColumns(['id', 'post_title', 'name AS category']),
new TermRelationshipCondition([
'taxonomy' => 'product_cat'
]),
]);
// $product->category will have the corresponding category name
// Fetch all products but override SELECT clause with only tree columns
$repository = $manager->getRepository(Product::class);
$result = $repository->createFindByQueryBuilder([], ['sku' => 'ASC'])
->select('id', 'post_title', select_from_eav('sku'))
->executeQuery()
->fetchAllAssociative();
$products = $repository->denormalize($result, Product::class . '[]');
// Create a new product category
$repository = $manager->getRepository(Term::class);
$term = $repository->createTermForTaxonomy('Jewelry', 'product_cat');
// Add all existing product tags to a product
$repository = $manager->getRepository(Term::class);
$repository->addTermsToEntity($product, $repository->findByTaxonomy('product_tag'));
// Remove all existing product tags from a product
$repository = $manager->getRepository(Term::class);
$repository->removeTermsFromEntity($product, $repository->findByTaxonomy('product_tag'));
$repository = $manager->getRepository(Post::class);
$repository->updatePostTitle(4, 'New title');
$repository->updatePostContent(4, 'New content');
$repository->updatePostDate(4, new \DateTime());
// Alternative
$repository->updateSingleField(4, 'post_status', 'publish');
$repository = $manager->getRepository(Post::class);
$post = $repository->findOneByPostTitle('My post');
$post->postTitle = 'A new title for my post';
$post->postStatus = 'publish';
$repository->persist($post);
// or directly calling the EntityManager
$manager->persist($post);
$duplicationService = $registry->get(DuplicationService::class);
// or
$duplicationService = DuplicationService::create($manager);
// Duplicate by ID
$newProduct = $duplicationService->duplicate(23, Product::class);
// Duplicate by object
$product = $manager->getRepository(Product::class)->findOneBySku('woo-hoodie-with-zipper');
$newProduct = $duplicationService->duplicate($product);
// Query the option name yourself
$blogName = $manager->getRepository(Option::class)->find('blogname');
// Use a predefined getter
$blogName = $manager->getRepository(Option::class)->findBlogName();
// If there isn't a predefined getter, use a magic method.
// Here we get the 'active_plugins' option, automatically unserialized.
$plugins = $manager->getRepository(Option::class)->findActivePlugins();
// App/Wordpress/Entity/Project.php
namespace App\Wordpress\Entity;
use App\Wordpress\Repository\ProjectRepository;
use Williarin\WordpressInterop\Attributes\RepositoryClass;
use Williarin\WordpressInterop\Bridge\Entity\BaseEntity;
#[RepositoryClass(ProjectRepository::class)]
final class Project extends BaseEntity
{
}
// App/Wordpress/Repository/ProjectRepository.php
namespace App\Wordpress\Repository;
use App\Wordpress\Entity\Project;
use Symfony\Component\Serializer\SerializerInterface;
use Williarin\WordpressInterop\Bridge\Repository\AbstractEntityRepository;
use Williarin\WordpressInterop\EntityManagerInterface;
/**
* @method Project|null find($id)
* @method Project[] findAll()
*/
final class ProjectRepository extends AbstractEntityRepository
{
public function __construct(/* inject additional services if you need them */)
{
parent::__construct(Project::class);
}
protected function getPostType(): string
{
return 'project';
}
// Add your own methods here
}