Skip to content

Instantly share code, notes, and snippets.

@epierpont
Last active June 14, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epierpont/d21ea29e16af93218ac32148538efff7 to your computer and use it in GitHub Desktop.
Save epierpont/d21ea29e16af93218ac32148538efff7 to your computer and use it in GitHub Desktop.
WP - Custom tax query for NSP Advanced Post Types Order
<?php
/*
I needed a way to override sort list query rules in the admin with my own custom tax queries.
So I created a list using the default query rules, saved the unique reorder sort ID to a page as a custom field
and used it to alter and assign the new tax query.
NSP Advanced Post Types Order Plugin - http://www.nsp-code.com/advanced-post-types-order-api/sample-usage/
*/
add_filter( 'apto_interface_query_args', 'linkReorderLists', 10, 2 );
function linkReorderLists( $args, $sort_view_id ) {
// get reorder sort ID that is being passed
$sortID = $args['sort_id'];
// find page where I'm saving unique reorder sort ID as custom field
$reorderCarousels = new WP_Query( array(
'post_type' => 'page',
'meta_key' => 'carousel_sort_id',
'meta_value' => $sortID )
);
// run through custom tax query, this will now link to $sortID
if ( $reorderCarousels->have_posts() ) {
while ( $reorderCarousels->have_posts() ) {
$reorderCarousels->the_post();
$args['tax_query'] = array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'include_children' => false,
'terms' => array( 'example-cat-1', 'example-cat-2' ),
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'example-cat-3', 'example-cat-4' ),
),
));
}
}
wp_reset_postdata();
return $args;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment