Last active
February 21, 2024 21:34
-
-
Save igorbenic/dddd6df40a8eac24ac4d8ca949148fdb to your computer and use it in GitHub Desktop.
Extending WP_Query with Custom Queries and Tables | https://www.ibenic.com/extending-wp-query-custom-queries-tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_clauses', 'filter_clauses', 10, 2 ); | |
/** | |
* Filtering everything. | |
* | |
* @param array $clauses Array with all parts of the query. | |
* @param WP_Query $wp_query Object. | |
* @return string | |
*/ | |
function filter_clauses( $clauses, $wp_query ) { | |
$clauses['where'] = " AND post.post_title LIKE '%SOMETEXT%'"; | |
return $clauses; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_join', 'ss_join_table', 20, 2 ); | |
/** | |
* Join custom tables. | |
* | |
* @param string $join String containing all joins. | |
* @param WP_Query $wp_query object. | |
* | |
* @return string | |
*/ | |
function ss_join_table( $join, $wp_query ) { | |
global $wpdb; | |
if ( isset( $wp_query->query['ss_package'] ) && absint( $wp_query->query['ss_package'] ) ) { | |
$join .= " INNER JOIN $wpdb->sssponsorships as ss_sponsorships on ss_sponsorships.sponsor = $wpdb->posts.ID"; | |
} | |
return $join; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_join', 'ss_join_table', 20, 2 ); | |
/** | |
* Join custom tables. | |
* | |
* @param string $join String containing all joins. | |
* @param WP_Query $wp_query object. | |
* | |
* @return string | |
*/ | |
function ss_join_table( $join, $wp_query ) { | |
global $wpdb; | |
// ... Previous code | |
if ( isset( $wp_query->query['ss_content'] ) && absint( $wp_query->query['ss_content'] ) ) { | |
$join .= " INNER JOIN $wpdb->postmeta as ss_post_meta on ss_post_meta.meta_value = $wpdb->posts.ID"; | |
} | |
return $join; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_where', 'ss_where_table', 20, 2 ); | |
/** | |
* Add the where clauses. | |
* | |
* @param string $where Where string. | |
* @param WP_Query $wp_query object. | |
* | |
* @return string | |
*/ | |
function ss_where_table( $where, $wp_query ) { | |
global $wpdb; | |
if ( isset( $wp_query->query['ss_package'] ) && absint( $wp_query->query['ss_package'] ) ) { | |
$where .= $wpdb->prepare( " AND ss_sponsorships.package=%d", absint( $wp_query->query['ss_package'] ) ); | |
} | |
return $where; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'posts_where', 'ss_where_table', 20, 2 ); | |
/** | |
* Add the where clauses. | |
* | |
* @param string $where Where string. | |
* @param WP_Query $wp_query object. | |
* | |
* @return string | |
*/ | |
function ss_where_table( $where, $wp_query ) { | |
global $wpdb; | |
// ... Previous code | |
if ( isset( $wp_query->query['ss_content'] ) && absint( $wp_query->query['ss_content'] ) ) { | |
$where .= $wpdb->prepare( " AND ss_post_meta.post_id=%d AND ss_post_meta.meta_key='_ss_sponsor'", absint( $wp_query->query['ss_content'] ) ); | |
} | |
return $where; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Hook filters. | |
* | |
* @return void | |
*/ | |
function plugin_filter_custom_param() { | |
add_filter( 'posts_where', 'plugin_cpt_filter_where', 20, 2 ); | |
add_filter( 'posts_join', 'plugin_cpt_filter_join', 20, 2 ); | |
add_filter( 'posts_distinct', 'plugin_cpt_filter_distinct', 20, 2 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get a custom CPT with get_posts. | |
* | |
* @param array $args Array of arguments. | |
* @return array Array of posts. | |
*/ | |
function plugin_get_cpt( $args = array() ) { | |
$args = wp_parse_args( $args, array( | |
'post_type' => 'your_cpt', | |
'posts_per_page' => '-1', | |
'your_custom_param' => '', | |
)); | |
if ( isset( $args['your_custom_param'] ) && $args['your_custom_param'] ) { | |
$args['suppress_filters'] = false; | |
plugin_filter_custom_param(); | |
} | |
$posts = get_posts( $args ); | |
if ( isset( $args['your_custom_param'] ) && $args['your_custom_param'] ) { | |
plugin_unfilter_custom_param(); | |
} | |
return $posts; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Unhook filters. | |
* | |
* @return void | |
*/ | |
function plugin_unfilter_custom_param() { | |
remove_filter( 'posts_where', 'plugin_cpt_filter_where', 20 ); | |
remove_filter( 'posts_join', 'plugin_cpt_filter_join', 20 ); | |
remove_filter( 'posts_distinct', 'plugin_cpt_filter_distinct', 20 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment