| 1 |
<?php |
| 2 |
function iwp_fn_get_posts_by($input = '', $field = 'post_title', $post_type = 'post') |
| 3 |
{ |
| 4 |
$core_fields = array( |
| 5 |
'ID', |
| 6 |
'menu_order', |
| 7 |
'comment_status', |
| 8 |
'ping_status', |
| 9 |
'pinged', |
| 10 |
'post_author', |
| 11 |
'post_category', |
| 12 |
'post_content', |
| 13 |
'post_date', |
| 14 |
'post_date_gmt', |
| 15 |
'post_excerpt', |
| 16 |
'post_name', |
| 17 |
'post_parent', |
| 18 |
'post_password', |
| 19 |
'post_status', |
| 20 |
'post_title', |
| 21 |
'post_type', |
| 22 |
'tags_input', |
| 23 |
'to_ping', |
| 24 |
'tax_input' |
| 25 |
); |
| 26 |
|
| 27 |
$query_vars = array( |
| 28 |
'post_name' => 'name', |
| 29 |
'ID' => 'p' |
| 30 |
); |
| 31 |
|
| 32 |
$parts = array_values(array_filter(array_map('trim', explode(',', $input)))); |
| 33 |
$output = []; |
| 34 |
foreach ($parts as $part) { |
| 35 |
|
| 36 |
$query_args = array( |
| 37 |
'post_type' => $post_type, |
| 38 |
'post_status' => 'any, trash, future', |
| 39 |
'fields' => 'ids', |
| 40 |
'cache_results' => false, |
| 41 |
'update_post_meta_cache' => false, |
| 42 |
'update_post_term_cache' => false, |
| 43 |
'no_found_rows' => true, |
| 44 |
); |
| 45 |
|
| 46 |
if (in_array($field, $core_fields, true)) { |
| 47 |
|
| 48 |
if (array_key_exists($field, $query_vars)) { |
| 49 |
$query_args[$query_vars[$field]] = $part; |
| 50 |
} else { |
| 51 |
switch ($field) { |
| 52 |
case 'post_title': |
| 53 |
$query_args['title'] = $part; |
| 54 |
break; |
| 55 |
default: |
| 56 |
$query_args[$field] = $part; |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
} else { |
| 61 |
$meta_args[] = array( |
| 62 |
'key' => $field, |
| 63 |
'value' => $part |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
if (!empty($meta_args)) { |
| 68 |
$query_args['meta_query'] = $meta_args; |
| 69 |
} |
| 70 |
|
| 71 |
$query = new \WP_Query($query_args); |
| 72 |
if (count($query->posts) === 1) { |
| 73 |
$output[] = $query->posts[0]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
return implode(',', $output); |
| 79 |
} |
| 80 |
|
| 81 |
function iwp_fn_prefix_items($input = '', $prefix = '', $output_delimiter = ',', $input_delimter = ',') |
| 82 |
{ |
| 83 |
$items = explode($input_delimter, $input); |
| 84 |
|
| 85 |
$items = array_map(function ($item) use ($prefix) { |
| 86 |
return $prefix . trim($item); |
| 87 |
}, $items); |
| 88 |
|
| 89 |
return implode($output_delimiter, $items); |
| 90 |
} |