title: How to use AJAX to change pagination in Wordpress
body: <p>I'm designing a wordpress childtheme based on thematic for a client in which the main work portfolio pagination has to be able to change every time the user clicks on "Order by year", "Order by title" and "Order by collection" (using taxonomy pagination) all using ajax.
  I've coded some functions following these <a href="http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/" rel="nofollow noreferrer">two</a> <a href="http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/" rel="nofollow noreferrer">posts</a> and these <a href="https://stackoverflow.com/questions/2908823/wordpress-how-to-call-a-plugin-function-with-an-ajax-call">two</a> <a href="https://wordpress.stackexchange.com/questions/13277/wp-ajax-question-not-using-wp-enqueue-script">answers</a>, but it is not working. I'm completely new to ajax so please be patient with me!
  Any suggestions for a better approach to keep it DRY are very much appreciated.
  Thank you very much in advance.</p>
  
  <p><strong>UPDATE</strong>
  I've solved the problem myself. I was calling different classes, all the procedure was fine. Corrected code below.</p>
  
  <p><strong>init.js:</strong></p>
  
  <pre><code>$(function(){
  
  var obra_links = $(".select_date, .select_title, .select_collection");
  var obra_archive = $(".obra_archive");
  
  obra_links.bind('click', function() {
      var $archive_class = $(this).attr("class");
      $(this).parent().find('.selected').removeClass('selected');
      $(this).toggleClass('selected');
      obra_archive.fadeOut(500);
      $.ajax(
          MyAjax.url,{
              type: 'POST',
              cache: false,
              data: {
                  action: 'obra_date_toggle',
                  archive_class: $archive_class
              },
              success: function(new_archive){
                  obra_archive.html(new_archive);
              },
          })
      obra_archive.fadeIn(500);
  }, false);
  });
  </code></pre>
  
  <p><strong>functions.php:</strong></p>
  
  <pre><code>`[...]`
  
  function childtheme_override_head_scripts() {
  if ( !is_admin() ) {
  // jQuery Google APIs
      wp_deregister_script('jquery');
      wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false);
      wp_enqueue_script('jquery');
  // Custom
      wp_enqueue_script('init', JS . '/init.js', array('jquery'), false, false); // Change last value to true but separate typekit.
  // AJAX
      wp_localize_script( 'init', 'MyAjax', array( 'url' =&gt; admin_url( 'admin-ajax.php' ) ) );
  } add_action('init', 'childtheme_override_head_scripts');
  
  function select_date() {
  // New Query
  $args = array(
      'post_type' =&gt; 'obra',
      'posts_per_page' =&gt; '-1',
      'orderby' =&gt; 'date',
      'order' =&gt; 'ASC',
      //'orderby' =&gt; 'title',
      //'taxonomy' =&gt; 'colecciones'
  ); $wp_query = new WP_Query( $args );
  
  if ($wp_query`-&gt;`have_posts()) {
      while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;
          &lt;div &lt;?php post_class() ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
              &lt;h2&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
              &lt;small&gt;&lt;?php the_time('F jS, Y') ?&gt; &lt;!-- by &lt;?php the_author() ?&gt; --&gt;&lt;/small&gt;
              &lt;div class="entry"&gt;
                &lt;?php the_content('Read the rest of this entry &amp;raquo;'); ?&gt;
              &lt;/div&gt;
              &lt;p class="postmetadata"&gt;&lt;?php the_tags('Tags: ', ', ', '&lt;br /&gt;'); ?&gt; Posted in &lt;?php the_category(', ') ?&gt; | &lt;?php edit_post_link('Edit', '', ' | '); ?&gt;  &lt;?php comments_popup_link('No Comments ', '1 Comment ', '% Comments '); ?&gt;&lt;/p&gt;
          &lt;/div&gt;&lt;?php
      endwhile;
  } else { ?&gt;
      &lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
      &lt;p class="center"&gt;Sorry, but you are looking for something that isn't here.&lt;/p&gt;
      &lt;?php get_search_form();
  }
  }
  
  function select_title() {
  `[...]`
  }
  
  function select_collection() {
  `[...]`
  }
  
  
  function obra_date_toggle() {
  if(isset($_POST['archive_class'])) {
      $archive_class = $_POST['archive_class'];
  }
  switch ($archive_class) {
      case "select_date":
          select_date();
          die();
          break;
  
      case "select_title":
          select_title();
          die();
          break;
  
      case "select_collection":
          select_collection();
          die();
          break;
  
      default:
          echo select_date();
          // For initial load, don't die
  }
  
  } add_action( 'wp_ajax_nopriv_obra_date_toggle', 'obra_date_toggle' );
  add_action( 'wp_ajax_obra_date_toggle', 'obra_date_toggle' );
  </code></pre>
  
  <p><strong>obra.php:</strong></p>
  
  <pre><code>&lt;?php
  /*
  Template Name: Obra
  */
  
  // calling the header.php
  get_header();
  
  // action hook for placing content above #container
  thematic_abovecontainer();
  
  ?&gt;
  
      &lt;div id="container"&gt;
  
          &lt;?php thematic_abovecontent(); ?&gt;
  
          &lt;div id="content"&gt;
              &lt;p&gt;
                  &lt;a class="select_date" href="#"&gt;Ordenar por fecha&lt;/a&gt;&lt;br /&gt;
                  &lt;a class="select_title" href="#"&gt;Ordenar por t&amp;iacute;tulo&lt;/a&gt;&lt;br /&gt;
                  &lt;a class="select_collection" href="#"&gt;Ordenar por colecci&amp;oacute;n&lt;/a&gt;&lt;br /&gt;
              &lt;/p&gt;
  
              &lt;div class="obra_archive"&gt;&lt;?php
  
                  obra_date_toggle();
  
              ?&gt;&lt;/div&gt;&lt;!-- .obra-archive --&gt;&lt;?php
  
                  wp_reset_postdata();
  
                  // calling the widget area 'page-top'
                  get_sidebar('page-top');
  
                  // calling the widget area 'page-bottom'
                  get_sidebar('page-bottom');
  
              ?&gt;&lt;/div&gt;&lt;!-- #content --&gt;
  
          &lt;?php thematic_belowcontent(); ?&gt; 
  
      &lt;/div&gt;&lt;!-- #container --&gt;
  
  &lt;?php 
  
  // action hook for placing content below #container
  thematic_belowcontainer();
  
  // calling the standard sidebar 
  thematic_sidebar();
  
  // calling footer.php
  get_footer();
  
  ?&gt;
  </code></pre>