This question is actually an extension of this question.
For default post type posts are populated based on category selection from the drop down, but when I change the settings for the Custom post types than posts do not populate for the CPT categories election from the drop down.
I think that the ajax part of the scripting is working fine. Check the visual here. But the conditional loop has some issue.
Here is the code →
/ Script for getting posts
function ajax_filter_get_posts( $taxonomy ) {
// Verify nonce
if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
die('Permission denied');
$taxonomy = $_POST['taxonomy'];
if($taxonomy == 'all'){
// WP Query
$args = array(
'post_type' => 'video',
'posts_per_page' => -1,
);
}else{
// WP Query
$args = array(
'category_name' => $taxonomy,
'post_type' => 'video',
'posts_per_page' => -1,
);
}
// If taxonomy is not set, remove key from array and get all posts
if( !$taxonomy ) {
unset( $args['category'] );
}
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="box">
<div class="images"><?php the_post_thumbnail( 'large') ?></div>
<div class="text3">
<h4><?php $categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<a class="themelor" href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
} ?></h4>
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
</div>
</li>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts found</h2>
<?php endif;
//die();
exit();
}
add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
I think this part is executing →
if($taxonomy == 'all'){
// WP Query
$args = array(
'post_type' => 'video',
'posts_per_page' => -1,
);
}else{
But this part is not executing →
$args = array(
'category_name' => $taxonomy,
'post_type' => 'video',
'posts_per_page' => -1,
Can some one help me where am I going wrong? what need to be fixed in the loop?
Extra:
Category for CPT is defined like this:
add_action( 'init', 'create_video_cpt_taxonomy' );
function create_video_cpt_taxonomy() {
register_taxonomy(
'vcategory',
'video',
array(
'label' => __( 'Video Category' ),
'rewrite' => array( 'slug' => 'vcategory' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'video_CPT_registration' );
For custom post types do we have to write this part in some other way:
$taxonomy = $_POST['taxonomy'];
Or perhaps there is something else that is conflicting.
Please let me Know if I have to give some extra information.
Update as requested →
<?php
wp_dropdown_categories( array(
'show_option_all' => 'All',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1, // change to 0 if empty terms should be shown to
'echo' => 1,
'selected' => 0,
'name' => 'categoryfilter',
'taxonomy' => 'vcategory',
'value_field' => 'slug',
) );
?>