forked from importwp/importwp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
90 lines (78 loc) · 2.3 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
<?php
function iwp_fn_get_posts_by($input = '', $field = 'post_title', $post_type = 'post')
{
$core_fields = array(
'ID',
'menu_order',
'comment_status',
'ping_status',
'pinged',
'post_author',
'post_category',
'post_content',
'post_date',
'post_date_gmt',
'post_excerpt',
'post_name',
'post_parent',
'post_password',
'post_status',
'post_title',
'post_type',
'tags_input',
'to_ping',
'tax_input'
);
$query_vars = array(
'post_name' => 'name',
'ID' => 'p'
);
$parts = array_values(array_filter(array_map('trim', explode(',', $input))));
$output = [];
foreach ($parts as $part) {
$query_args = array(
'post_type' => $post_type,
'post_status' => 'any, trash, future',
'fields' => 'ids',
'cache_results' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'no_found_rows' => true,
);
if (in_array($field, $core_fields, true)) {
if (array_key_exists($field, $query_vars)) {
$query_args[$query_vars[$field]] = $part;
} else {
switch ($field) {
case 'post_title':
$query_args['title'] = $part;
break;
default:
$query_args[$field] = $part;
break;
}
}
} else {
$meta_args[] = array(
'key' => $field,
'value' => $part
);
}
if (!empty($meta_args)) {
$query_args['meta_query'] = $meta_args;
}
$query = new \WP_Query($query_args);
if (count($query->posts) === 1) {
$output[] = $query->posts[0];
}
}
return implode(',', $output);
}
function iwp_fn_prefix_items($input = '', $prefix = '', $output_delimiter = ',', $input_delimter = ',')
{
$items = explode($input_delimter, $input);
$items = array_map(function ($item) use ($prefix) {
return $prefix . trim($item);
}, $items);
return implode($output_delimiter, $items);
}