First, your custom post type is portfolio-item
, not portfolio_item
. Note that you register the post type with $this->base
name and previously you did $this->base="portfolio-item";
. Also, numberposts
is obsolete, use post_per_page
instead:
$args = array(
// 'post_type' => 'portfolio_item',
'post_type' => 'portfolio-item',
'orderby' => 'rand',
'post_per_page' => 1
);
Then, the taxomy argument of your query is wrong. You must use tax_query
argument as follow:
if ( isset( $_POST['usecategory'] ) && intval( $_POST['usecategory'] ) != 0 ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio-category',
'terms' => intval( $_POST['usecategory'] )
)
);
}
$posts = get_posts( $args );
By default, the arguments exepct terms IDs. If $_POST['usecategory']
is not the term ID but term slug:
if ( isset( $_POST['usecategory'] ) && intval( $_POST['usecategory'] ) != 0 ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'slug',
'terms' => $_POST['usecategory']
)
);
}
You can all possible combinations in the Codex: WP_Query: taxomomy parameters.
Also, your jQuery has syntax error, you missed });
:
jQuery(document).ready(function($) {
$('.grp_getnew').on('click', function(e){
e.preventDefault();
var data = {
action: 'get_random_post_tu',
usecategory: $('#categoryselect').val()
};
$.post( ajax_object.ajax_url, data, function(response) {
if ( response.status != 'error' ) {
var $link = $("<a href="" + response.link + "">" + response.title + "</a><span >" + response.content +"</span>");
$('.grp_content').html($link);
}
}, "json");
});
});