Skip to content

Commit b6696bc

Browse files
committed
feat(Fns): Added meta getters
1 parent ff6fdfe commit b6696bc

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

Post.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace XWP\Helper\Functions;
4+
5+
use WP_Post;
6+
7+
class Post {
8+
public static function get_by_type_and_meta( string $meta_key, mixed $value, string $post_type, string $retval = 'ID' ): int|WP_Post|null {
9+
global $wpdb;
10+
$res = (int) $wpdb->get_var(
11+
$wpdb->prepare(
12+
<<<'SQL'
13+
SELECT p.ID FROM %i AS p
14+
INNER JOIN %i AS pm
15+
ON p.ID = pm.post_id
16+
AND pm.meta_key = %s
17+
AND pm.meta_value = %s
18+
WHERE p.post_type = %s
19+
LIMIT 1
20+
SQL,
21+
$wpdb->posts,
22+
$wpdb->postmeta,
23+
$meta_key,
24+
$value,
25+
$post_type,
26+
),
27+
);
28+
29+
if ( 'ID' === $retval ) {
30+
return $res;
31+
}
32+
33+
return $res ? \get_post( $res ) : null;
34+
}
35+
36+
public static function get_by_meta( string $meta_key, mixed $value, string $retval = 'ID' ): int|WP_Post|null {
37+
global $wpdb;
38+
39+
$res = (int) $wpdb->get_var(
40+
$wpdb->prepare(
41+
<<<'SQL'
42+
SELECT post_id FROM %i
43+
WHERE meta_key = %s
44+
AND meta_value = %s
45+
LIMIT 1
46+
SQL,
47+
$wpdb->postmeta,
48+
$meta_key,
49+
$value,
50+
),
51+
);
52+
53+
if ( 'ID' === $retval ) {
54+
return $res;
55+
}
56+
57+
return $res ? \get_post( $res ) : null;
58+
}
59+
}

Term.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,60 @@
1010
* Taxonomy and term helper functions.
1111
*/
1212
final class Term {
13+
public static function get_by_tax_and_meta( string $meta_key, mixed $value, string $taxonomy, string $retval = 'ID' ): int|WP_Term|null {
14+
global $wpdb;
15+
16+
$res = (int) $wpdb->get_var(
17+
$wpdb->prepare(
18+
<<<'SQL'
19+
SELECT tt.term_id
20+
FROM %i AS tt
21+
INNER JOIN %i AS tm
22+
ON tt.term_id = tm.term_id
23+
AND tm.meta_key = %s
24+
AND tm.meta_value = %s
25+
WHERE tt.taxonomy = %s
26+
LIMIT 1
27+
SQL,
28+
$wpdb->term_taxonomy,
29+
$wpdb->termmeta,
30+
$meta_key,
31+
$value,
32+
$taxonomy,
33+
),
34+
);
35+
36+
if ( 'ID' === $retval ) {
37+
return $res;
38+
}
39+
40+
return $res ? \get_term( $res ) : null;
41+
}
42+
43+
public static function get_by_meta( string $meta_key, mixed $value, string $retval = 'ID' ): int|WP_Term|null {
44+
global $wpdb;
45+
46+
$res = (int) $wpdb->get_var(
47+
$wpdb->prepare(
48+
<<<'SQL'
49+
SELECT term_id FROM %i
50+
WHERE meta_key = %s
51+
AND meta_value = %s
52+
LIMIT 1
53+
SQL,
54+
$wpdb->termmeta,
55+
$meta_key,
56+
$value,
57+
),
58+
);
59+
60+
if ( 'ID' === $retval ) {
61+
return $res;
62+
}
63+
64+
return $res ? \get_term( $res ) : null;
65+
}
66+
1367
/**
1468
* Formats a term name with its ancestors.
1569
*

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
},
3333
"files": [
3434
"xwp-helper-fns-arr.php",
35+
"xwp-helper-fns-meta.php",
3536
"xwp-helper-fns-num.php",
3637
"xwp-helper-fns-req.php",
3738
"xwp-helper-fns.php"

xwp-helper-fns-meta.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Meta helper functions definition file.
4+
*
5+
* @package eXtended WordPress
6+
* @subpackage Helper\Functions
7+
*/
8+
9+
use XWP\Helper\Functions as f;
10+
11+
if ( ! function_exists( 'xwp_get_post_by_meta' ) ) :
12+
/**
13+
* Gets a post by its meta key and value.
14+
*
15+
* @param string $meta_key Meta key to search for.
16+
* @param mixed $value Meta value to search for.
17+
* @param 'ID'|'object' $retval Return value type, either 'ID' for post ID or 'object' for WP_Post object.
18+
* @return ($retval is 'ID ? int<0,max> : null|WP_Post)
19+
*/
20+
function xwp_get_post_by_meta( string $meta_key, mixed $value, string $retval = 'ID' ): int|WP_Post|null {
21+
return f\Post::get_by_meta( $meta_key, $value, $retval );
22+
}
23+
endif;
24+
25+
if ( ! function_exists( 'xwp_get_post_by_type_and_meta' ) ) :
26+
/**
27+
* Gets a post by its type and meta key and value.
28+
*
29+
* @param string $meta_key Meta key to search for.
30+
* @param mixed $value Meta value to search for.
31+
* @param string $post_type Post type to search for.
32+
* @param 'ID'|'object' $retval Return value type, either 'ID' for post ID or 'object' for WP_Post object.
33+
* @return ($retval is 'ID ? int<0,max> : null|WP_Post)
34+
*/
35+
function xwp_get_post_by_type_and_meta( string $meta_key, mixed $value, string $post_type, string $retval = 'ID' ): int|WP_Post|null {
36+
return f\Post::get_by_type_and_meta( $meta_key, $value, $post_type, $retval );
37+
}
38+
endif;
39+
40+
41+
if ( ! function_exists( 'xwp_get_term_by_meta' ) ) :
42+
/**
43+
* Gets a term by its meta key and value.
44+
*
45+
* @param string $meta_key Meta key to search for.
46+
* @param mixed $value Meta value to search for.
47+
* @param 'ID'|'object' $retval Return value type, either 'ID' for term ID or 'object' for WP_Term object.
48+
* @return ($retval is 'ID ? int<0,max> : null|WP_Term)
49+
*/
50+
function xwp_get_term_by_meta( string $meta_key, mixed $value, string $retval = 'ID' ): int|WP_Term|null {
51+
return f\Term::get_by_meta( $meta_key, $value, $retval );
52+
}
53+
endif;
54+
55+
if ( ! function_exists( 'xwp_get_term_by_tax_and_meta' ) ) :
56+
/**
57+
* Gets a term by its taxonomy, meta key and value.
58+
*
59+
* @param string $meta_key Meta key to search for.
60+
* @param mixed $value Meta value to search for.
61+
* @param string $taxonomy Taxonomy to search for.
62+
* @param 'ID'|'object' $retval Return value type, either 'ID' for term ID or 'object' for WP_Term object.
63+
* @return ($retval is 'ID ? int<0,max> : null|WP_Term)
64+
*/
65+
function xwp_get_term_by_tax_and_meta( string $meta_key, mixed $value, string $taxonomy, string $retval = 'ID' ): int|WP_Term|null {
66+
return f\Term::get_by_tax_and_meta( $meta_key, $value, $taxonomy, $retval );
67+
}
68+
endif;

0 commit comments

Comments
 (0)