Skip to content

Instantly share code, notes, and snippets.

@conorseed
Last active August 27, 2021 19:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save conorseed/57e0974b3c38948e47bb3ac248af5886 to your computer and use it in GitHub Desktop.
Save conorseed/57e0974b3c38948e47bb3ac248af5886 to your computer and use it in GitHub Desktop.
<?php
/*
** CUSTOM LOOPER FOR PRO THEME
** 1. Set "Looper Provider" as "Custom" and the hook as "get_my_query"
** 2. Add below function to functions.php in child theme
** 3. Set Params via JSON in Pro. Takes "post_type" and "tax_query" in format here:
{
"post_type": ["post", "tv_show"],
"tax_query": {
"relation": "OR",
"query": [{
"taxonomy": "category",
"terms": "news",
"field": "slug"
},
{
"taxonomy": "tv_show_category",
"terms": "tester",
"field": "slug"
}
]
}
}
*/
add_filter( 'cs_looper_custom_get_my_query', function( $result, $params ) {
// WP_Query arguments
$args = array();
// Check if post type is in params.
// Otherwise default to post
$post_type = ( isset($params['post_type']) ) ? $params['post_type'] : 'post';
$args['post_type'] = $post_type;
// Check if post type is in params.
// Otherwise default to post
$tax_query = ( isset($params['tax_query']) ) ? $params['tax_query'] : '';
// Organise Tax Query
if($tax_query){
foreach($tax_query['query'] as $i => $v){
array_push($tax_query, $v);
}
unset($tax_query['query']);
$args['tax_query'] = $tax_query;
}
// The Query
$query = new WP_Query( $args );
// Return
return $query->posts;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment