Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kcor555 committed Aug 2, 2022
0 parents commit 60d9a7e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
17 changes: 17 additions & 0 deletions composer.json
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": {}
}
44 changes: 44 additions & 0 deletions readme.md
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.
// ...
)
);
```
18 changes: 18 additions & 0 deletions wp-query-starts-with.php
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
);

0 comments on commit 60d9a7e

Please sign in to comment.