-
Notifications
You must be signed in to change notification settings - Fork 23
/
class.pmpro_posts_by_content.php
66 lines (53 loc) · 2.01 KB
/
class.pmpro_posts_by_content.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Created by Thomas Sjolshagen at PMPro <[email protected]>
* Copyright 2016 (c) - Stranger Studios, LLC
*
* Class pmpro_posts_by_content is based on code from:
* http://wordpress.stackexchange.com/questions/49549/how-to-get-posts-by-content/49556#49556
*/
class pmpro_posts_by_content
{
protected static $content = '';
protected static $like = true;
/**
* Mapper function for get_posts() supporting extra arguments 'content' and 'like'
*
* @param 'content' => String w/optional wildcard ('%') for free values
* @param 'like' => true or false
*
* @return WP_Post
* @since .4
*/
public static function get( $args )
{
if ( isset( $args['content'] ) ) {
// 'suppress_filters' is TRUE by default for the get_posts() function
// it needs to be FALSE if we need the 'WHERE' filter to work
$args['suppress_filters'] = false;
self::$content = $args['content'];
add_filter('posts_where', array(__CLASS__, 'where_filter'));
}
// isset( $args['like'] ) and self::$like = (bool) $like;
$posts = get_posts( $args);
if( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG )
error_log("pmpro_posts_by_content: " . print_r($posts, true));
return $posts;
}
public static function where_filter( $where )
{
remove_filter('posts_where', array(__CLASS__, 'where_filter'));
if( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG )
error_log("pmpro_posts_by_content: " . print_r( $where, true ));
global $wpdb;
$like = self::$like ? 'LIKE' : 'NOT LIKE';
$extra = $wpdb->prepare('%s', self::$content);
// reset variables
self::$content = '';
self::$like = true;
$new_where = "{$where} AND post_content {$like} {$extra}";
if( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG )
error_log("pmpro_posts_by_content: " . print_r($new_where, true));
return $new_where;
}
}