0

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',
            ) );
        ?>
3
  • 1
    hmm is that being submitted via a form, or is there JS code?
    – Tom J Nowell
    Jun 1, 2018 at 13:24
  • 1
    I think its Ajax/JQuery. Like this, and in functions.php like this
    – WordCent
    Jun 1, 2018 at 18:11
  • You should include the actual code you used as well as were it's located in your question for reference, links die/change/move, you can embed images in questions but it would be better to embed code blocks instead
    – Tom J Nowell
    Jun 6, 2018 at 14:32

1 Answer 1

1

But this part is not executing →

$args = array(
    'category_name' => $taxonomy,
    'post_type' => 'video',
    'posts_per_page' => -1,

It probably is. It's just wrong.

If you want to query posts by terms in a custom taxonomy you need to add a taxonomy query:

$args = array(
    'post_type'      => 'video',
    'posts_per_page' => -1,
    'tax_query'      => array(
        array(
            'taxonomy' => 'vcategory',
            'field'    => 'slug',
            'terms'    => $taxonomy,
        )
    )
);

This is explained, with examples, in the documentation. I'm going to implore you again to please read documentation.

Also, you're using the wrong $_POST field. In wp_dropdown_categories() you've given the field the name categoryfilter, but are trying to get the value for taxonomy. You need to set $taxonomy like this:

$taxonomy = $_POST['categoryfilter];

Also, you shouldn't be using POST for this type of request. You should be using GET:

GET is used to request data from a specified resource.

POST is used to send data to a server to create/update a resource.

https://www.w3schools.com/tags/ref_httpmethods.asp

You're doing the first thing.

3
  • Hi there, Thanks. Here we will use category or vcategory?
    – WordCent
    Jun 1, 2018 at 16:31
  • #2 → Now when I have implemented things the category drop downs also pulls all the posts not just category selected. Can you guide me how should I troubleshoot? Like this
    – WordCent
    Jun 2, 2018 at 5:20
  • 1
    Please fix this 'tax_query' => array() to 'tax_query' => array(array()) One more array under array. so that I can accept the answer.
    – WordCent
    Jun 3, 2018 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.