Just one note before I start, you should not call your post type menu
, menu
is a reserved term, so you might run into issues with default build in functionalities. You can, however, use the plural form menus
without having to worry about issues ragarding conflicts
As for your code, you are using invalid parameters, which is just simply ignored by WP_Query
which in turn is the reason you are getting back all posts regardless. For custom taxonomies, you should use a tax_query
to query posts belonging to custom taxonomy terms
The following should work: (Note: You just need to feed the correct taxonomy name and correct term ID to the tax_query
for your specific use case)
$posts = get_posts(array(
'post_type' => 'menu',
'orderby' => 'name',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => $wcatTerm->taxonomy,
'terms' => $wcatTerm->term_id,
'include_children' => false,
)
),
'nopaging' => true,
));
EDIT
The category parameters will not work as you are not making use of the build in taxonomy, category
. You are using a custom taxonomy, so to query posts from a specific term or terms from a custom taxonomy, you need to use a tax_query
as in my sample code.
As I also stated, you need to feed the correct parameters to your tax_query
as I do not know your exact setup. The terms
parameter (by default) should be an integer value or an array of integer values of term ID’s, so take that into account
Lets take an example from your code
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
Inside this foreach
loop, your tax_query
values will be as follow:
-
Your
taxonomy
value will be$wsc->taxonomy
-
Your
terms
value will be(int) $wsc->term_id
EDIT 2
Sifting through your code, there are plenty errors. I have corrected all that errors in your code and have commented on them inside the code, so make sure to check my comments. Where I have stated to remove, remove that specific line of code as it is not necessary. Also, I do not know if you really need empty terms, so I have left that and just build in protection to avoid bugs and errors if there are actually empty terms
<?php
$taxonomy = 'menu-items'; // Do not use hypens in taxonomy names, only use underscores
$term_args = array(
'hide_empty' => 0, // Why do you need empty terms?
'orderby' => 'ASC',
'parent' => 0
);
$wcatTerms = get_terms(
$taxonomy, // Taxonomy name
$term_args // Array of arguments to get terms by
);
// To avoid bugs and errors, run this only if we have a valid array of terms
if ( !empty( $wcatTerms ) // Check that we do not have an empty array
&& !is_wp_error( $wcatTerms ) // Check that we do not have a WP_Error object returned
) {
// If our conditional check passed, then only run the following code
foreach($wcatTerms as $wcatTerm) { // Do not use : as it is hard to debug and to read, use old style curlies
//echo $wcatTerm->name;
$wsubargs = array(
//'hierarchical'=>1, // This is default, not needed and can be removed
//'show_option_none'=>'', // Invalid parameter, where do you get this from, to remove
'hide_empty'=> 0, // Why do you need empty terms?
'parent'=>$wcatTerm->term_id,
);
//$wsubcats = get_categories($wsubargs); // Why use get_categories, get_categories uses get_terms, to remove
$wsubcats = get_terms(
$taxonomy, // We have set this earlier
$wsubargs
);
foreach ( $wsubcats as $wsc ) { // Again, do not use : as it is hard to debug
?><h2><?php //echo $wsc->name; ?></h2>
<?php $childcat = $wsc->name; ?>
<h2><?php echo $childcat; ?></h2>
<?php
//$posts = get_posts(array( // Do not mess with $posts global, rather use $posts_array, to remove
$args = array(
'post_type' => 'menu', // Really bad name, reserved name in WordPress
'orderby' => 'name',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy, // We have defined this already right at the start
'terms => $wsc->term_id,
'include_children' => false,
)
),
'nopaging' => true,
);
$posts_array = get_posts( $args );
// You are missing the loop here, that is why you are getting the same stuff over and over again
if ( $posts_array ) { // Only run this if we actually have posts, if we do not have posts, we will have bugs and errors
foreach ( $posts_array as $post ) { // You were missing this
setup_postdata( $post ); // VERY VERY IMPORTANT, without this, the template tags like the_content will not work
?>
<div class="menu-title"><?php the_title(); ?></div>
<div class="menu-title"><?php the_content(); ?></div>
<?php } // endforeach $posts_array
wp_reset_postdata(); // VERY IMPORTANT to reset the $post global to main query
} // endif $posts_array
} // endforeach $wsubcats
} // endforeach $wcatTerms
} // endif check for valid array of terms
?>