-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 60d9a7e
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "wp-forge/wp-query-starts-with", | ||
"description": "A Composer package for WordPress that allows you to query posts based on what the title starts with.", | ||
"type": "library", | ||
"license": "GPL-2.0-or-later", | ||
"authors": [ | ||
{ | ||
"name": "Kris Cochran" | ||
} | ||
], | ||
"autoload": { | ||
"files": [ | ||
"wp-query-starts-with.php" | ||
] | ||
}, | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# WordPress Query Starts With | ||
|
||
<a href="https://wordpress.org/" target="_blank"> | ||
<img src="https://img.shields.io/static/v1?label=&message=2.8+-+6.0&color=blue&style=flat-square&logo=wordpress&logoColor=white" alt="WordPress Versions"> | ||
</a> | ||
<a href="https://www.php.net/" target="_blank"> | ||
<img src="https://img.shields.io/static/v1?label=&message=5.2+-+8.1&color=777bb4&style=flat-square&logo=php&logoColor=white" alt="PHP Versions"> | ||
</a> | ||
|
||
A Composer package for WordPress that allows you to query posts based on what the title starts with. | ||
|
||
## Installation | ||
|
||
Install [Composer](https://getcomposer.org/). | ||
|
||
In your WordPress plugin or theme directory, run: | ||
|
||
``` | ||
composer require wp-forge/wp-query-starts-with | ||
``` | ||
|
||
Make sure you have this line of code in your project: | ||
|
||
```php | ||
<?php | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
``` | ||
|
||
## Usage | ||
|
||
When creating a custom query: | ||
|
||
```php | ||
<?php | ||
|
||
$query = new WP_Query( | ||
array( | ||
'post_type' => 'post', | ||
'starts_with' => 'Pre', // This is case-sensitive and must match the first part of the post title exactly. | ||
// ... | ||
) | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
add_filter( | ||
'posts_where', | ||
function ( $where, WP_Query $query ) { | ||
global $wpdb; | ||
|
||
$starts_with = esc_sql( $query->get( 'starts_with' ) ); | ||
|
||
if ( $starts_with ) { | ||
$where .= " AND {$wpdb->posts}.post_title LIKE '{$starts_with}%'"; | ||
} | ||
|
||
return $where; | ||
}, | ||
10, | ||
2 | ||
); |