PHP code example of pollora / query
1. Go to this page and download the library: Download pollora/query 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/ */
pollora / query example snippets
use Pollora\Query\PostQuery;
$results = PostQuery::select()
->postType('product')
->taxQuery(function (TaxQuery $query) {
$query->where(
$query
->taxonomy('category')
->contains(['tee-shirt', 'sportswear'])
->searchByTermSlug()
);
})
->dateQuery(function (DateQuery $query) {
$query->where(
$query
->date('posted_at')
->between('2021-01-01', '2022-02-01')
)->orWhere(
$query->date()->created()->after('2021-01-01')
);
})
->metaQuery(function (MetaQuery $query) {
$query
->where(
$query->meta('status')->equalTo('active')
)->orWhere(
$query->meta('highlighted')->equalTo(0)
);
})
->userPermission('readable')
->cacheResults()
->get();
new WP_Query([
'cache_results' => true,
'date_query' => [
'relation' => 'OR',
[
'column' => 'posted_at',
'before' =>
[
'year' => '2022',
'month' => '02',
'day' => '01',
],
'after' =>
[
'year' => '2021',
'month' => '01',
'day' => '01',
],
],
[
'column' => 'post_date',
'after' =>
[
'year' => '2021',
'month' => '01',
'day' => '01',
],
],
],
'fields' => 'all',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'status',
'compare' => '=',
'value' => 'active',
],
[
'key' => 'highlighted',
'compare' => '=',
'type' => 'BINARY',
'value' => 0,
],
],
'perm' => 'readable',
'post_type' => 'product',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' =>
[
'tee-shirt',
'sportswear',
],
'operator' => 'IN',
'
PostQuery::select()->get(); // Equivalent to PostQuery::select('*')->get();
new WP_Query([
'fields' => 'all',
'post_type' => 'any',
]);
PostQuery::select('id=>parent')->get();
new WP_Query([
'fields' => 'id=>parent',
'post_type' => 'all',
]);
PostQuery::select('ids')->get();
new WP_Query([
'fields' => 'ids',
'post_type' => 'all',
]);
PostQuery::find(1) // you can also pass an array of post ids.
->fields('ids')
->get();
new WP_Query([
'p' => 1, // Retrieve the post with ID 1
'fields' => 'ids', // Retrieve only the post ID
'post_type' => 'all',
]);
PostQuery::select()
->author(1)
->get();
new WP_Query([
'author' => 1, // Retrieve posts authored by the user with ID 1
'post_type' => 'all' // Include all post types in the query
]);
PostQuery::select()
->authorName('taylor')
->get();
new WP_Query([
'author_name' => 'taylor', // Retrieve posts authored by the user with the username 'taylor'
'post_type' => 'all' // Include all post types in the query
]);
PostQuery::select()
->authorIn([1, 2, 3])
->get();
new WP_Query([
'author__in' => [1, 2, 3], // Retrieve posts authored by users with IDs 1, 2, or 3
'post_type' => 'all' // Include all post types in the query
]);
PostQuery::select()
->authorNotIn([1, 2, 3])
->get();
new WP_Query([
'author__not_in' => [1, 2, 3], // Exclude posts authored by users with IDs 1, 2, or 3
'post_type' => 'all' // Include all post types in the query
]);
PostQuery::select()
->cat(1)
->get();
new WP_Query([
'cat' => 1, // Retrieve posts from category with ID 1
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->categoryName('sales')
->get();
new WP_Query([
'category_name' => 'sales', // Retrieve posts from the 'sales' category
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->categoryIn([1, 2, 3])
->get();
new WP_Query([
'category__in' => [1, 2, 3], // Include posts from categories with IDs 1, 2, or 3
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->categoryNotIn([1, 2, 3])
->get();
new WP_Query([
'category__not_in' => [1, 2, 3], // Exclude posts from categories with IDs 1, 2, or 3
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tag('programming')
->get();
new WP_Query([
'tag' => 'programming', // Retrieve posts associated with the 'programming' tag
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagId(1)
->get();
new WP_Query([
'tag_id' => 1, // Retrieve posts associated with the tag with ID 1
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagAnd([1, 2])
->get();
new WP_Query([
'tag__and' => [1, 2], // Retrieve posts associated with both tags with IDs 1 and 2
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagIn([3, 4])
->get();
new WP_Query([
'tag__in' => [3, 4], // Retrieve posts associated with tags with IDs 3 or 4
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagNotIn([5, 6])
->get();
new WP_Query([
'tag__not_in' => [5, 6], // Exclude posts associated with tags with IDs 5 or 6
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagSlugAnd(['dev', 'qa'])
->get();
new WP_Query([
'tag_slug__and' => ['dev', 'qa'], // Retrieve posts associated with both 'dev' and 'qa' tags
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->tagSlugIn(['frontend', 'backend'])
->get();
new WP_Query([
'tag_slug__in' => ['frontend', 'backend'], // Retrieve posts associated with 'frontend' or 'backend' tags
'post_type' => 'all', // Include all post types in the query
]);
PostQuery::select()
->taxQuery(function (TaxQuery $query) {
$query->where(
$query
->taxonomy('category')
->contains(['tee-shirt', 'sportswear'])
->searchByTermSlug()
);
})
->get();
new WP_Query([
'post_type' => 'all',
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'category',
'field' => 'slug',
'terms' => ['tee-shirt', 'sportswear'],
'operator' => 'IN',
'
PostQuery::select()
->taxQuery(function (TaxQuery $query) {
$query->where(
$query
->taxonomy('category')
->contains([10, 20])
->searchById()
)->orWhere(
$query
->taxonomy('event')
->contains(['black-friday', 'christmas-sales'])
->searchByTermSlug()
);
})
->get();
new WP_Query([
'post_type' => 'all',
'tax_query' => [
'relation' => 'OR',
[
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [10, 20],
'operator' => 'IN',
'
PostQuery::select()
->taxQuery(function (TaxQuery $query) {
$query
->where(
$query->taxonomy('status')->notContains(['private'])
)
->orWhere(function (TaxQuery $query) {
$query
->where(
$query->taxonomy('attributes')->exists()
)
->orWhere(
$query->taxonomy('product_cat')->contains(['tee-shirt', 'sportswear'])
);
})
->orWhere(function (TaxQuery $query) {
$query
->where(
$query->taxonomy('promo_cat')->contains(['summer', 'blackfriday'])
)
->andWhere(
$query->taxonomy('new_products')->notExists()
);
});
})
->get();
new WP_Query([
'post_type' => 'all',
'tax_query' => [
'relation' => 'OR',
[
'taxonomy' => 'status',
'field' => 'term_id',
'terms' => [
'private',
],
'operator' => 'NOT IN',
' [
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => [
'tee-shirt',
'sportswear',
],
'operator' => 'IN',
'
'
PostQuery::select()
->search('my keyword')
->get();
new WP_Query([
's' => 'my keyword', // Perform a keyword-based search for posts containing 'my keyword'
'post_type' => 'all',
]);
PostQuery::select()
->postType('article')
->get();
new WP_Query([
'post_type' => 'article', // Retrieve posts of the 'article' post type
]);
PostQuery::select()
->postId(42)
->get();
new WP_Query([
'p' => 42, // Retrieve the post with ID 42
'post_type' => 'all'
]);
PostQuery::select()
->postSlug('mon-article')
->get();
new WP_Query([
'name' => 'mon-article', // Retrieve the post with the slug 'mon-article'
'post_type' => 'all'
]);
PostQuery::select()
->postParent(5)
->get();
new WP_Query([
'post_parent' => 5, // Retrieve posts with a parent post of ID 5
'post_type' => 'all',
]);
PostQuery::select()
->whereParentIn([1, 2, 3])
->get();
new WP_Query([
'post_parent__in' => [1, 2, 3], // Retrieve posts with parent post IDs in the array
'post_type' => 'all'
]);
PostQuery::select()
->whereParentNotIn([4, 5, 6])
->get();
new WP_Query([
'post_parent__not_in' => [4, 5, 6], // Exclude posts with parent post IDs in the array
'post_type' => 'all'
]);
PostQuery::select()
->whereIn([7, 8, 9])
->get();
new WP_Query([
'post__in' => [7, 8, 9], // Retrieve posts with IDs in the array
'post_type' => 'all'
]);
PostQuery::select()
->whereNotIn([10, 11, 12])
->get();
new WP_Query([
'post__not_in' => [10, 11, 12], // Exclude posts with IDs in the array
'post_type' => 'all'
]);
PostQuery::select()
->slugIn(['slug-1', 'slug-2'])
->get();
new WP_Query([
'post_name__in' => ['slug-1', 'slug-2'], // Retrieve posts with slugs in the array
'post_type' => 'all'
]);
PostQuery::select()
->postType('page')
->postSlug('my-page')
->get();
new WP_Query([
'post_type' => 'page', // Query for pages
'name' => 'my-page', // Specify the page slug
'post_type' => 'all'
]);
PostQuery::select()
->withPassword()
->get();
new WP_Query([
'has_password' => true, // Retrieve posts with password protection
'post_type' => 'all'
]);
PostQuery::select()
->withoutPassword()
->get();
new WP_Query([
'has_password' => false, // Exclude posts with password protection
'post_type' => 'all'
]);
PostQuery::select()
->withPassword('zxcvbn')
->get();
new WP_Query([
'post_password' => 'zxcvbn', // Retrieve posts with the specified post password
'post_type' => 'all'
]);
PostQuery::select()
->postStatus('publish')
->get();
new WP_Query([
'post_status' => 'publish', // Retrieve posts with the 'publish' status
'post_type' => 'all'
]);
PostQuery::select()
->commentCount(1)
->get();
new WP_Query([
'comment_count' => 1, // Retrieve posts with exactly 1 comment
]);
PostQuery::select()
->take(5)
->get();
new WP_Query([
'posts_per_page' => 5, // Retrieve 5 posts per page
]);
PostQuery::select()
->skip(5)
->get();
new WP_Query([
'offset' => 5, // Skip the first 5 posts in the query results
]);
PostQuery::select()
->noPaging()
->get();
new WP_Query([
'nopaging' => true, // Retrieve all posts without pagination
]);
PostQuery::select()
->postsPerArchivePage(5)
->get();
new WP_Query([
'posts_per_archive_page' => 5, // Display 5 posts per archive page
]);
PostQuery::select()
->page(666)
->get();
new WP_Query([
'page' => 666, // Retrieve results for page number 666
]);
PostQuery::select()
->ignoreStickyPosts()
->get();
new WP_Query([
'ignore_sticky_posts' => true, // Exclude sticky posts from the query
]);
PostQuery::select()
->orderBy('most_comments')
->orderBy('post_date', 'ASC')
->get();
new WP_Query([
'orderby' => [
'most_comments' => 'DESC', // Order by 'most_comments' in descending order
'post_date' => 'ASC', // Then order by 'post_date' in ascending order
],
]);
PostQuery::select()->dateQuery(function (DateQuery $query) {
$query
->where(
$query->date('edited_at')->before('2022-01-01')->after([
'year' => '2021',
'month' => '01',
'day' => '01',
])
)
->inclusive();
})->get();
new WP_Query([
'date_query' => [
'relation' => 'AND',
[
'column' => 'edited_at',
'before' => [
'year' => 2022,
'month' => 1,
'day' => 1,
'hour' => 12,
'minute' => 0,
'second' => 0,
],
'after' => [
'year' => 2021,
'month' => 1,
'day' => 1,
],
],
'inclusive' => true,
],
]);
$query->date('edited_at');
PostQuery::select()->metaQuery(function (MetaQuery $query) {
$query->where(
$query->meta('status')->equalTo('active')
);
})->get();
new WP_Query([
'meta_query' => [
'relation' => 'AND',
[
'key' => 'status',
'compare' => '=',
'type' => 'CHAR',
'value' => 'active',
],
],
]);
$query->meta('my_post_meta')->ofType('numeric'); // You can also use a number
$query->meta('my_post_meta')->equalTo('active'); // You can also use a number
$query->meta('my_post_meta')->notEqualTo('inactive');
php
$query->meta('my_post_meta')->between('2022-01-01', '2022-12-31');
php
$query->meta('my_post_meta')->notBetween('2022-01-01', '2022-12-31');
php
PostQuery::select()
->metaQuery(function (MetaQuery $query) {
$query
->where(
$query
->meta('menu_order')
->state('menu_order_state')
->greaterThan(10)
);
})
->orderBy('menu_order_state', 'ASC')
->get();
php
PostQuery::select()->metaQuery(function (MetaQuery $query) {
$query
->where(
$query->meta('status')->equalTo('active')
)
->orWhere(
$query->meta('highlighted')->equalTo(1)
);
})->get();
php
PostQuery::select()
->metaQuery(function (MetaQuery $query) {
$query
->where(
$query->meta('status')->equalTo('active')
)
->orWhere(function (MetaQuery $query) {
$query
->where(
$query->meta('_sku')->equalTo('H123456789')
)
->orWhere(
$query->meta('_sku')->equalTo('D123456784')
);
})
->orWhere(function (MetaQuery $query) {
$query
->where(
$query->meta('promo_cat')->notIn(['summer', 'blackfriday'])->state('promo_cat')
)
->andWhere(
$query->meta('promo_cat')->notEqualTo('sales')
);
})
->orWhere(
$query->meta('sale_date')
->between('2024-01-01', '2024-12-31')
);
})
->orderBy('promo_cat')
->get();
php
PostQuery::select()
->userPermission('readable')
->get();
php
new WP_Query([
'perm' => 'readable',
'post_type' => 'all',
]);
php
PostQuery::select()
->userPermission('editable')
->get();
php
new WP_Query([
'perm' => 'editable',
'post_type' => 'all',
]);
php
PostQuery::select()
->userPermission('invalid')
->get();
php
new WP_Query([
'post_type' => 'all',
]);
php
PostQuery::select()
->postMimeType('image/gif')
->get();
php
new WP_Query([
'post_mime_type' => 'image/gif',
'post_type' => 'all',
]);
php
PostQuery::select()
->postMimeType(['image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon'])
->get();
php
PostQuery::select()
->cacheResults()
->get();
php
PostQuery::select()
->updateMetaCache(false)
->get();
php
new WP_Query([
'update_post_meta_cache' => false,
'post_type' => 'all',
]);
php
PostQuery::select()
->updateTermCache(true)
->get();
php
new WP_Query([
'update_post_term_cache' => true,
'post_type' => 'all',
]);