Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 21, 2024 21:34
Show Gist options
  • Save igorbenic/dddd6df40a8eac24ac4d8ca949148fdb to your computer and use it in GitHub Desktop.
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
<?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;
}
<?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;
}
<?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;
}
<?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;
}
<?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;
}
<?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 );
}
<?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;
}
<?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