forked from alleyinteractive/es-wp-query
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
91 lines (82 loc) · 3.31 KB
/
functions.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* ES_WP_Query functions
*
* @package ES_WP_Query
*/
// phpcs:disable WordPressVIPMinimum.Files.IncludingFile.IncludingFile
if ( ! function_exists( 'es_get_posts' ) ) {
/**
* Retrieve list of latest posts or posts matching criteria.
*
* The defaults are as follows:
* 'numberposts' - Default is 5. Total number of posts to retrieve.
* 'offset' - Default is 0. See {@link WP_Query::query()} for more.
* 'category' - What category to pull the posts from.
* 'orderby' - Default is 'date', which orders based on post_date. How to order the posts.
* 'order' - Default is 'DESC'. The order to retrieve the posts.
* 'include' - See {@link WP_Query::query()} for more.
* 'exclude' - See {@link WP_Query::query()} for more.
* 'meta_key' - See {@link WP_Query::query()} for more.
* 'meta_value' - See {@link WP_Query::query()} for more.
* 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
* 'post_parent' - The parent of the post or post type.
* 'post_status' - Default is 'publish'. Post status to retrieve.
*
* @uses WP_Query::query() See for more default arguments and information.
* @uses ES_WP_Query
* @link http://codex.wordpress.org/Template_Tags/get_posts
*
* @param array $args Optional. Overrides defaults.
* @return array List of posts.
*/
function es_get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'offset' => 0,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '', // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key
'meta_value' => '', // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_value
'post_type' => 'post',
'suppress_filters' => true, // phpcs:ignore WordPressVIPMinimum.VIP.WPQueryParams.suppressFiltersTrue
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) ) {
$r['post_status'] = ( 'attachment' === $r['post_type'] ) ? 'inherit' : 'publish';
}
if ( ! empty( $r['numberposts'] ) && empty( $r['posts_per_page'] ) ) {
$r['posts_per_page'] = $r['numberposts'];
}
if ( ! empty( $r['category'] ) ) {
$r['cat'] = $r['category'];
}
if ( ! empty( $r['include'] ) ) {
$incposts = wp_parse_id_list( $r['include'] );
$r['posts_per_page'] = count( $incposts ); // Only the number of posts included.
$r['post__in'] = $incposts;
} elseif ( ! empty( $r['exclude'] ) ) {
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); // phpcs:ignore WordPressVIPMinimum.VIP.WPQueryParams.post__not_in
}
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new ES_WP_Query();
return $get_posts->query( $r );
}
}
/**
* Loads one of the included adapters.
*
* @param string $adapter Which adapter to include. Currently allows searchpress, wpcom-vip, travis, jetpack-search, and vip-search.
* @return void
*/
function es_wp_query_load_adapter( $adapter ) {
if ( 'vip-search' === $adapter ) {
require_once ES_WP_QUERY_PATH . "/adapters/vip-search.php";
} else {
_doing_it_wrong( __FUNCTION__, "Search: Only the vip-search adapter is available to use within Enterprise Search!", null );
}
}