badcrocodile
2/7/2014 - 4:08 PM

Create new wp_query via shortcode (paginated and non paginated)

Create new wp_query via shortcode (paginated and non paginated)

<?php
/*
Plugin Name: Custom WP_Query shortcode
Plugin URI: http://goshen.edu/tuts/plugins/custom-wp_query-shortcode/
Description: Adds shortcodes to create custom loops inside posts or pages. Read <a href="http://www.goshen.edu/tuts/plugins/custom-wp_query-shortcode/">usage instructions</a> for more details.
Author: Jason Pollock
Version: 1.1
Author URI: http://goshen.edu/com-mar/meet-the-staff/
*/
function custom_query_shortcode($atts) {
  // Plugin sourced from http://digwp.com/2010/01/custom-query-shortcode/
  // EXAMPLE USAGE: [loop the_query="showposts=100&post_type=page&post_parent=453"]
  extract(shortcode_atts(array(
    "the_query" => ''
  ), $atts));
  $paged = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 );
  // de-funkify query
  $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
  $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
  $the_query .=  $paged;
  query_posts($the_query);
  $output = '';
  $temp_content = '';
  if (have_posts()) : while (have_posts()) : the_post();
    $temp_content = get_the_content();
    $output .= $temp_content;
  endwhile; else:
    $output .= "<p>No posts have been written yet.</p>";
  endif;
  if ( function_exists( 'wp_pagenavi' ) ) :
    $output .= '<div class="pagination">';
    ob_start();
    wp_pagenavi();
    $output .= ob_get_clean();
    $output .= '</div>';
  endif;
  wp_reset_query();
  return $output;
}
add_shortcode("loop", "custom_query_shortcode");

/* Use the following shortcode if you need to display post title, author, and publish date */
function full_custom_query_shortcode($atts) {
  // EXAMPLE USAGE: [fullloop the_query='posts_per_page=12&post_type=post&category_name=student-blog&paged=']
  extract(shortcode_atts(array(
    "the_query" => ''
  ), $atts));
  // Set up our pagination
  $paged = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 );
  // de-funkify query
  $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
  $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
  $the_query .=  $paged;
  query_posts($the_query);
  $output = '';
  $temp_title = '';
  $temp_link = '';
  $temp_author = '';
  $temp_pub_date = '';
  $temp_author_link = '';
  $temp_content = '';
  if (have_posts()) : while (have_posts()) : the_post();
    $temp_title = get_the_title();
    $temp_link = get_permalink();
    $temp_author = get_the_author();
    $temp_author_link = get_author_posts_url( get_the_author_meta( 'ID' ) );
    $temp_pub_date = get_the_date();
    $temp_content = get_the_content();
    $temp_content = apply_filters('the_content', $temp_content);
    $temp_content = str_replace(']]>', ']]&gt;', $temp_content);
    $output .= "<div class='blog-post recent-post archive-post'>";
    $output .= "<div class='post-title'>";
    $output .= "<div class='no-push'><h2><a class='themed' href='$temp_link'>$temp_title</a></h2></div>";
    $output .= "<div class='post-byline'>";
    $output .= "<span class='post-author'>Written by</span> <a href='$temp_author_link'>$temp_author</a> ";
    $output .= "<span class='post-date'>on</span> $temp_pub_date</div>";
    $output .= "</div>";
    $output .= "</div>";
    $output .= $temp_content;
  endwhile; else:
    $output .= "<p>No articles have been written yet. Please check back soon!</p>";
  endif;
  if ( function_exists( 'wp_pagenavi' ) ) :
    $output .= '<div class="pagination">';
    ob_start();
    wp_pagenavi();
    $output .= ob_get_clean();
    $output .= '</div>';
  endif;
  wp_reset_query();
  return $output;
}
add_shortcode("fullloop", "full_custom_query_shortcode");

function simple_custom_query_shortcode($atts) {
  // A simple custom loop without pagination
  // EXAMPLE USAGE: [loop the_query="showposts=100&post_type=page&post_parent=453"]
  extract(shortcode_atts(array(
    "the_query" => ''
  ), $atts));
  // de-funkify query
  $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
  $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
  query_posts($the_query);
  $output = '';
  $temp_title = '';
  $temp_link = '';
  if (have_posts()) : while (have_posts()) : the_post();
    $temp_title = get_the_title();
    $temp_link = get_permalink();
    $output .= "<li class=\"simple_loop\"><a href='$temp_link'>$temp_title</a></li>";
  endwhile; else:
    $output .= "nothing found.";
  endif;
  wp_reset_query();
  return $output;
}
add_shortcode("simple_loop", "simple_custom_query_shortcode");


function gc_custom_query_nopage($atts) {
  extract(shortcode_atts(array(
    "the_query" => ''
  ), $atts));
  // de-funkify query
  $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
  $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);
  query_posts($the_query);
  $output = '';
  $temp_link = '';
  $temp_title = '';
  $temp_content = '';
  if (have_posts()) : while (have_posts()) : the_post();
    $temp_link = get_permalink();
    $temp_title = get_the_title();
    $temp_pub_date = get_the_date();
    $temp_content = get_the_content();
    $temp_content = apply_filters('the_content', $temp_content);
    $temp_content = str_replace(']]>', ']]&gt;', $temp_content);
    $output .= "<div class=\"loop_post_open\">";
    $output .= "<h2><a href='$temp_link'>$temp_title</a></h2>";
    $output .= "<span class='post-date'>Posted on:</span> $temp_pub_date";
    $output .= $temp_content;
    $output .= "</div>";
  endwhile; else:
    $output .= "nothing found.";
  endif;
  wp_reset_query();
  return $output;
}
add_shortcode("loop_nopage", "gc_custom_query_nopage");
?>