javascript – How to paginate custom post type Ajax results-ThrowExceptions

Exception or error:

So I’m currently working on a search form for custom post types. I’m searching for taxonomies and custom fields (the search is working pretty well) but I just can’t get the pagination to work.

Does anybody know how to handle the pagination on results?

So here’s my plugin code:

<?php
/*
Plugin Name: camera Saf
Description: Filterform
Version: 1.0
*/

function camera_searchandfilter(){
 ?>
 <div class = "row saf">
     <div class = "filterform">

        <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

    <?php
     /*camerae*/
        if( $terms = get_terms( array( 'taxonomy' => 'camera', 'orderby' => 'name' ) ) ) :
        echo '<div class = "col-12">';
            echo '<select name="cameraselect"><option value="">camera...</option>';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->slug . '">' . $term->name . '</option>'; // ID of the category as the value of an option
            endforeach;
            echo '</select>';
        endif;

      /*Kategorien*/
       if( $terms2 = get_terms( array( 'taxonomy' => 'kategorie', 'orderby' => 'name' ) ) ) :
         echo '<select name="categoryselect"><option value="">Kategorie...</option>';
         foreach ( $terms2 as $term2 ) :
           if($term2->name == '74') :
                  echo '<option value="' . $term2->slug . '"> 1 </option>';
              endif;
           if($term2->name == '83') :
                  echo '<option value="' . $term2->slug . '"> 2 </option>';
              endif;
            if($term2->name == '73') :
                 echo '<option value="' . $term2->slug . '"> 3 </option>';
              endif;
           //echo '<option value="' . $term2->slug . '">' . $term2->name . '</option>'; // ID of the category as the value of an option
         endforeach;
         echo '</select>';
       endif;

       /*lens*/
       if( $terms = get_terms( array( 'taxonomy' => 'lens', 'orderby' => 'name' ) ) ) :

         echo '<select name="lensselect"><option value="">lens...</option>';
         foreach ( $terms as $term ) :
           echo '<option value="' . $term->slug . '">' . $term->name . '</option>'; // ID of the category as the value of an option
         endforeach;
         echo '</select>';
         echo '</div>';
       endif;
      echo '<input type = "checkbox" name = "iso_cbx" value="iso">iso</option></center></div>';
      echo '<input type = "checkbox" name = "apperture_cbx" value="apperture">apperture</option></center></div>';
      echo '<input type = "checkbox" name = "mode_cbx" value="mode">mode</option></center></div>';
      echo '<input type = "checkbox" name = "ss_cbx" value="ss">ss</option></center></div>';
      echo '<input type = "checkbox" name = "sz_cbx" value="sz">sz</option></center></div>';
      echo '<input type = "checkbox" name = "wi_cbx" value="wi">wi</option></center></div>';
      echo '</div>';
    ?>

    <button class = "sendebtn">Suchen</button>
    <input type="hidden" name="action" value="myfilter">
   </form>
     </div>
   <div id="response"></div>
</div>
<?php
}
add_shortcode('camera_saf' , 'camera_searchandfilter');


add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
function misha_filter_function(){

$camera = $_POST['cameraselect'];
$category = $_POST['categoryselect'];
$lens = $_POST['lensselect'];
$wi = $_POST['wi_cbx'];
$sz = $_POST['sz_cbx'];
$mode = $_POST['mode_cbx'];
$apperture = $_POST['apperture_cbx'];
$iso = $_POST['iso_cbx'];
$ss = $_POST['ss_cbx'];

$taxquery = array();


$args = array(
  'post_type' => 'partner',
  'posts_per_page' => -1,
); // Get all posts

if(!empty($camera)){
  array_push($taxquery,array(
        'taxonomy' => 'camera',
        'field' => 'slug',
        'terms' => $camera,
    ));
}

if(!empty($category)){
  array_push($taxquery,array(
        'taxonomy' => 'kategorie',
        'field' => 'slug',
        'terms' => $category,
    ));
}

if(!empty($lens)){
  array_push($taxquery,array(
        'taxonomy' => 'lens',
        'field' => 'slug',
        'terms' => $lens,
    ));
}

if(!empty($wi)){
  $wi = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'wi',
        'field' => 'slug',
        'terms' => $wi,
    ));
}

if(!empty($sz)){
  $sz = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'sz',
        'field' => 'slug',
        'terms' => $sz,
    ));
}

if(!empty($ss)){
  $ss = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'ss',
        'field' => 'slug',
        'terms' => $ss,
    ));
}

if(!empty($mode)){
  $mode = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'mode',
        'field' => 'slug',
        'terms' => $mode,
    ));
}

if(!empty($apperture)){
  $apperture = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'apperture',
        'field' => 'slug',
        'terms' => $apperture,
    ));
}

if(!empty($iso)){
  $iso = 'WAHR';
  array_push($taxquery,array(
        'taxonomy' => 'iso',
        'field' => 'slug',
        'terms' => $iso,
    ));
}



if(!empty($taxquery)){
    $args['tax_query'] = $taxquery;
}

if(!empty($metaquery)){
  $args['meta_query'] = $metaquery;
}

$query = new WP_Query( $args );

if( $query->have_posts() ) :
  ?>
  <div class = "resultcontainer">
  <?php
    while( $query->have_posts() ): $query->the_post();
    ?>
  <div class = "singleresult">
    <div class = "logo">
    <?php
        echo the_post_thumbnail();?>
        </div>
        <div class = "moreinfo">
        <h2><?php echo $query->post->post_title; ?> </h2>
        <b> <?php
            $termsb = get_the_terms( $post->ID , 'camera' );
            $termsbl = get_the_terms($post->ID, 'lens');

            if ( $termsb != null ){
            foreach( $termsb as $termb ) {
              echo ucfirst($termb->slug);
            }
          }
          ?> </b>
        <?php
    $custom = get_post_custom();
    ?>
             <br><b> 
    <?php
    if(isset($custom['PLZ'])){
             echo $custom['PLZ'][0];
         }
    ?>
              </b>    
    <?php
         if(isset($custom['Ort'])){
             echo $custom['Ort'][0];
         }
    ?>
        <hr>
          <?php
            $terms = get_the_terms( $post->ID , 'kategorie' );
            if ( $terms != null ){
            foreach( $terms as $term ) {
              switch($term->slug){
                case '74':
                  echo '<b>Selection 1</b>';
                  break;
                case '83':
                  echo '<b>Selection 2</b>';
                  break;
                case '73':
                  echo '<b>Selection 3</b>';
                  break;
              }
            }
          }
          ?>

    </div>
</div>
          <?php
    endwhile;
    ?>
  </div> 
    <?php
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;

die();
}

function wpbootstrap_enqueue_styles() {
wp_enqueue_style( 'bootstrap', '//stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'wpbootstrap_enqueue_styles');

?>

And then I have a script.js:

jQuery(function($){
$('#filter').submit(function(){
    var filter = $('#filter');
    $.ajax({
        url:filter.attr('action'),
        data:filter.serialize(), // form data
        type:filter.attr('method'), // POST
        beforeSend:function(xhr){
            filter.find('button').text('Suche...'); // changing the button label
        },
        success:function(data){
            filter.find('button').text('Suchen'); // changing the button label back
            $('#response').html(data); // insert data
        }
    });
    return false;
});
});

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *