-
-
Save gnistdesign/fac70e9ea9c1378dcf08 to your computer and use it in GitHub Desktop.
Ajax Load More: Meta Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* WordPress Ajax Load More | |
* http://wordpress.org/plugins/ajax-load-more/ | |
* https://github.com/dcooney/wordpress-ajax-load-more | |
* | |
* Copyright 2014 Connekt Media - http://connekthq.com | |
* Free to use under the GPLv2 license. | |
* http://www.gnu.org/licenses/gpl-2.0.html | |
* | |
* Author: Darren Cooney | |
* Twitter: @KaptonKaos | |
*/ | |
(function($) { | |
"use strict"; | |
$.ajaxloadmore = function(el) { | |
//Set variables | |
var alm = this; | |
alm.AjaxLoadMore = {}; | |
alm.page = 0; | |
alm.speed = 300; | |
alm.proceed = false; | |
alm.init = true; | |
alm.loading = true; | |
alm.finished = false; | |
alm.window = $(window); | |
alm.button_label = ''; | |
alm.data; | |
alm.el = el; | |
alm.content = $('.alm-listing', alm.el); | |
alm.scroll = true; | |
alm.prefix = 'alm-'; | |
alm.repeater = alm.content.data('repeater'); | |
alm.max_pages = alm.content.data('max-pages'); | |
alm.pause = alm.content.data('pause'); | |
alm.offset = alm.content.data('offset'); | |
alm.transition = alm.content.data('transition'); | |
alm.lang = alm.content.data('lang'), | |
alm.posts_per_page = alm.content.data('posts-per-page'); | |
$(window).scrollTop(0); //Prevent loading of unnessasry posts - move user to top of page | |
// Check for pause on init | |
// Pause could be used to hold the loading of posts for a button click. | |
if (alm.pause === undefined) { | |
alm.pause = false; | |
} | |
// Select the repeater template | |
if (alm.repeater === undefined) { | |
alm.repeater = 'default'; | |
} | |
// Max number of pages to load while scrolling | |
if (alm.max_pages === undefined) { | |
alm.max_pages = 5; | |
} | |
if (alm.max_pages === 'none') { | |
alm.max_pages = 1000000; | |
} | |
// select the transition | |
if (alm.transition === undefined) { | |
alm.transition = 'slide'; | |
} else if (alm.transition === "fade") { | |
alm.transition = 'fade'; | |
} else { | |
alm.transition = 'slide'; | |
} | |
// Define offset | |
if (alm.content.data('offset') === undefined) { | |
alm.offset = 0; | |
} else { | |
alm.offset = alm.content.data('offset'); | |
} | |
// Define button text | |
if (alm.content.data('button-label') === undefined) { | |
alm.button_label = 'Older Posts'; | |
} else { | |
alm.button_label = alm.content.data('button-label'); | |
} | |
// Define on Scroll event | |
if (alm.content.data('scroll') === undefined) { | |
alm.scroll = true; | |
} else if (alm.content.data('scroll') === false) { | |
alm.scroll = false; | |
} else { | |
alm.scroll = true; | |
} | |
// Parse multiple Post Types | |
alm.post_type = alm.content.data('post-type'); | |
alm.post_type = alm.post_type.split(","); | |
// Append 'load More' button to .ajax-load-more-wrap | |
alm.el.append('<div class="'+alm.prefix+'btn-wrap"><button id="load-more" class="'+alm.prefix+'load-more-btn more">' + alm.button_label + '</button></div>'); | |
alm.button = $('.alm-load-more-btn', alm.el); | |
/* loadPosts() | |
* | |
* The function to get posts via Ajax | |
* @since 2.0.0 | |
*/ | |
alm.AjaxLoadMore.loadPosts = function() { | |
alm.button.addClass('loading'); | |
alm.loading = true; | |
$.ajax({ | |
type: "GET", | |
url: alm_localize.ajaxurl, | |
data: { | |
action: 'ajax_load_more_init', | |
nonce: alm_localize.alm_nonce, | |
repeater: alm.repeater, | |
postType: alm.post_type, | |
postFormat: alm.content.data('post-format'), | |
category: alm.content.data('category'), | |
meta_query: alm.content.data('metaquery'), | |
author: alm.content.data('author'), | |
taxonomy: alm.content.data('taxonomy'), | |
taxonomy_terms: alm.content.data('taxonomy-terms'), | |
taxonomy_operator: alm.content.data('taxonomy-operator'), | |
tag: alm.content.data('tag'), | |
order: alm.content.data('order'), | |
orderby: alm.content.data('orderby'), | |
search: alm.content.data('search'), | |
exclude: alm.content.data('exclude'), | |
numPosts: alm.content.data('posts-per-page'), | |
pageNumber: alm.page, | |
offset: alm.offset, | |
lang: alm.lang | |
}, | |
dataType: "html", | |
// parse the data as html | |
beforeSend: function() { | |
if (alm.page != 1) { | |
alm.button.addClass('loading'); | |
} | |
}, | |
success: function(data) { | |
alm.data = $(data); // Convert data to an object | |
//console.log(alm.data.length); | |
if (alm.init) { | |
alm.button.text(alm.button_label); | |
alm.init = false; | |
} | |
if (alm.data.length > 0) { | |
alm.el = $('<div class="' + alm.prefix + 'reveal"/>'); | |
alm.el.append(alm.data); | |
alm.el.hide(); | |
alm.content.append(alm.el); | |
if (alm.transition === 'fade') { // Fade transition | |
alm.el.fadeIn(alm.speed, 'alm_easeInOutQuad', function() { | |
alm.loading = false; | |
alm.button.delay(alm.speed).removeClass('loading'); | |
if (alm.data.length < alm.posts_per_page) { | |
alm.finished = true; | |
alm.button.addClass('done'); | |
} | |
}); | |
} else { // Slide transition | |
alm.el.slideDown(alm.speed, 'alm_easeInOutQuad', function() { | |
alm.loading = false; | |
alm.button.delay(alm.speed).removeClass('loading'); | |
if (alm.data.length < alm.posts_per_page) { | |
alm.finished = true; | |
alm.button.addClass('done'); | |
} | |
}); | |
} | |
if ($.isFunction($.fn.almComplete)) { | |
$.fn.almComplete(alm); | |
} | |
} else { | |
alm.button.delay(alm.speed).removeClass('loading').addClass('done'); | |
alm.loading = false; | |
alm.finished = true; | |
} | |
}, | |
error: function(jqXHR, textStatus, errorThrown) { | |
alm.loading = false; | |
alm.button.removeClass('loading'); | |
} | |
}); | |
}; | |
/* Button onClick() | |
* | |
* Load more button click event | |
* @since 1.0.0 | |
*/ | |
alm.button.on('click', function() { | |
if(alm.pause === true){ | |
alm.pause = false; | |
alm.AjaxLoadMore.loadPosts(); | |
} | |
if (!alm.loading && !alm.finished && !$(this).hasClass('done')) { | |
alm.loading = true; | |
alm.page++; | |
alm.AjaxLoadMore.loadPosts(); | |
} | |
}); | |
/* AjaxLoadMore.isVisible() | |
* | |
* Check to see if element is visible before loading posts | |
* @since 2.1.2 | |
*/ | |
alm.AjaxLoadMore.isVisible = function(){ | |
alm.visible = false; | |
if(alm.el.is(":visible")){ | |
alm.visible = true; | |
} | |
return alm.visible; | |
}; | |
/* Window scroll and touchmove events | |
* | |
* Load posts as user scrolls the page | |
* @since 1.0 | |
*/ | |
if (alm.scroll) { | |
alm.window.bind("scroll touchstart", function() { | |
if(alm.AjaxLoadMore.isVisible()){ | |
var content_offset = alm.button.offset(); | |
if (!alm.loading && !alm.finished && alm.window.scrollTop() >= Math.round(content_offset.top - (alm.window.height() - 150)) && alm.page < (alm.max_pages - 1) && alm.proceed) { | |
alm.loading = true; | |
alm.page++; | |
alm.AjaxLoadMore.loadPosts(); | |
} | |
} | |
}); | |
} | |
//Check for pause variable | |
if(alm.pause === true){ | |
alm.button.text(alm.button_label); | |
alm.loading = false; | |
}else{ | |
alm.AjaxLoadMore.loadPosts(); | |
} | |
//flag to prevent unnecessary loading of post on init. Hold for 1 second | |
setTimeout(function() { | |
alm.proceed = true; | |
}, 1000); | |
//Custom easing function | |
$.easing.alm_easeInOutQuad = function(x, t, b, c, d) { | |
if ((t /= d / 2) < 1) return c / 2 * t * t + b; | |
return -c / 2 * ((--t) * (t - 2) - 1) + b; | |
}; | |
}; | |
// End $.ajaxloadmore | |
/* $.fn.ajaxloadmore() | |
* | |
* Initiate all instances of Ajax load More | |
* @since 2.1.2 | |
*/ | |
$.fn.ajaxloadmore = function() { | |
return this.each(function() { | |
$(this).data('alm', new $.ajaxloadmore($(this))); | |
}); | |
} | |
/* | |
* Initiate Ajax load More if div is present on screen | |
* @since 2.1.2 | |
*/ | |
if($(".ajax-load-more-wrap").length) | |
$(".ajax-load-more-wrap").ajaxloadmore(); | |
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Ajax Load More | |
Plugin URI: http://connekthq.com/plugins/ajax-load-more | |
Description: A simple solution for lazy loading WordPress posts and pages with Ajax | |
Author: Darren Cooney | |
Twitter: @KaptonKaos | |
Author URI: http://connekthq.com | |
Version: 2.2.2 | |
License: GPL | |
Copyright: Darren Cooney & Connekt Media | |
*/ | |
define('ALM_VERSION', '2.2.2'); | |
define('ALM_RELEASE', 'September 23, 2014'); | |
/* | |
* alm_install | |
* Create table for storing repeater | |
* | |
* @since 2.0.0 | |
*/ | |
register_activation_hook( __FILE__, 'alm_install' ); | |
function alm_install() { | |
global $wpdb; | |
$table_name = $wpdb->prefix . "alm"; | |
$defaultRepeater = '<li><?php if ( has_post_thumbnail() ) { the_post_thumbnail(array(100,100));}?><h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3><p class="entry-meta"><?php the_time("F d, Y"); ?></p><?php the_excerpt(); ?></li>'; | |
//Create table, if it doesn't already exist. | |
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { | |
$sql = "CREATE TABLE $table_name ( | |
id mediumint(9) NOT NULL AUTO_INCREMENT, | |
name text NOT NULL, | |
repeaterDefault longtext NOT NULL, | |
pluginVersion text NOT NULL, | |
UNIQUE KEY id (id) | |
);"; | |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
dbDelta( $sql ); | |
//Insert default data in newly created table | |
$wpdb->insert($table_name , array('name' => 'default', 'repeaterDefault' => $defaultRepeater, 'pluginVersion' => ALM_VERSION)); | |
} | |
// Updated 2.0.5 | |
// Check column 'name' exists in $wpdb - this is an upgrade checker. | |
$row = $wpdb->get_results("SELECT COLUMN_NAME FROM $table_name.COLUMNS WHERE column_name = 'name'"); | |
if(empty($row)){ | |
$wpdb->query("ALTER TABLE $table_name ADD name text NOT NULL"); | |
$wpdb->update($table_name , array('name' => 'default'), array('id' => 1)); | |
} | |
} | |
if( !class_exists('AjaxLoadMore') ): | |
class AjaxLoadMore { | |
function __construct(){ | |
define('ALM_PATH', plugin_dir_path(__FILE__)); | |
define('ALM_URL', plugins_url('', __FILE__)); | |
define('ALM_ADMIN_URL', plugins_url('admin/', __FILE__)); | |
define('ALM_NAME', '_ajax_load_more'); | |
define('ALM_TITLE', 'Ajax Load More'); | |
add_action('wp_ajax_ajax_load_more_init', array(&$this, 'alm_query_posts')); | |
add_action('wp_ajax_nopriv_ajax_load_more_init', array(&$this, 'alm_query_posts')); | |
add_action('wp_enqueue_scripts', array(&$this, 'alm_enqueue_scripts')); | |
add_action('alm_get_repeater', array(&$this, 'alm_get_current_repeater')); | |
add_shortcode('ajax_load_more', array(&$this, 'alm_shortcode')); | |
// Allow shortcodes in widget areas | |
add_filter('widget_text', 'do_shortcode'); | |
// load text domain | |
load_plugin_textdomain( 'ajax-load-more', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); | |
// includes Admin core | |
$this->alm_before_theme(); | |
} | |
/* | |
* alm_before_theme | |
* Load these files before the theme loads | |
* | |
* @since 2.0.0 | |
*/ | |
function alm_before_theme(){ | |
if( is_admin()){ | |
include_once('admin/editor.php'); | |
include_once('admin/admin.php'); | |
} | |
} | |
/* | |
* alm_enqueue_scripts | |
* Enqueue our scripts and create our localize variables | |
* | |
* @since 2.0.0 | |
*/ | |
function alm_enqueue_scripts(){ | |
$options = get_option( 'alm_settings' ); | |
wp_enqueue_script( 'ajax-load-more', plugins_url( '/core/js/ajax-load-more.js', __FILE__ ), array('jquery'), '1.1', true ); | |
wp_localize_script( | |
'ajax-load-more', | |
'alm_localize', | |
array( | |
'ajaxurl' => admin_url('admin-ajax.php'), | |
'alm_nonce' => wp_create_nonce( "ajax_load_more_nonce" ), | |
'pluginurl' => ALM_URL | |
) | |
); | |
if(!isset($options['_alm_disable_css']) || $options['_alm_disable_css'] != '1'){ | |
wp_enqueue_style( 'ajax-load-more-css', plugins_url('/core/css/ajax-load-more.css', __FILE__ )); | |
} | |
} | |
/* | |
* alm_shortcode | |
* The AjaxLoadMore shortcode | |
* | |
* @since 2.0.0 | |
*/ | |
function alm_shortcode( $atts, $content = null ) { | |
$options = get_option( 'alm_settings' ); //Get plugin options | |
extract(shortcode_atts(array( | |
'repeater' => 'default', | |
'post_type' => 'post', | |
'post_format' => '', | |
'category' => '', | |
'taxonomy' => '', | |
'taxonomy_terms' => '', | |
'taxonomy_operator' => '', | |
'tag' => '', | |
'author' => '', | |
'search' => '', | |
'order' => '', | |
'orderby' => '', | |
'exclude' => '', | |
'offset' => '0', | |
'posts_per_page' => '5', | |
'scroll' => 'true', | |
'max_pages' => '5', | |
'pause' => 'false', | |
'transition' => 'slide', | |
'button_label' => 'Older Posts', | |
'meta_query' => '' | |
), | |
$atts)); | |
// Get container elements (ul | div) | |
$container_element = 'ul'; | |
if($options['_alm_container_type'] == '2'){ | |
$container_element = 'div'; | |
} | |
// Get extra classnames | |
$classname = ''; | |
if(isset($options['_alm_classname'])){ | |
$classname = ' '.$options['_alm_classname']; | |
} | |
// Get button color | |
$btn_color = ''; | |
if(isset($options['_alm_btn_color'])){ | |
$btn_color = ' '.$options['_alm_btn_color']; | |
} | |
$lang = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : ''; | |
$ajaxloadmore = '<div id="ajax-load-more" class="ajax-load-more-wrap '. $btn_color .'">'; | |
$ajaxloadmore .= '<'.$container_element.' class="alm-listing'. $classname . '" data-repeater="'.$repeater.'" data-post-type="'.$post_type.'" data-post-format="'.$post_format.'" data-category="'.$category.'" data-metaquery="'.$meta_query.'" data-taxonomy="'.$taxonomy.'" data-taxonomy-terms="'.$taxonomy_terms.'" data-taxonomy-operator="'.$taxonomy_operator.'" data-tag="'.$tag.'" data-author="'.$author.'" data-search="'.$search.'" data-order="'.$order.'" data-orderby="'.$orderby.'" data-exclude="'.$exclude.'" data-offset="'.$offset.'" data-posts-per-page="'.$posts_per_page.'" data-lang="'.$lang.'" data-scroll="'.$scroll.'" data-max-pages="'.$max_pages.'" data-pause="'. $pause .'" data-button-label="'.$button_label.'" data-transition="'.$transition.'"></'.$container_element.'>'; | |
$ajaxloadmore .= '</div>'; | |
return $ajaxloadmore; | |
} | |
/* | |
* alm_query_posts | |
* Ajax Load More Public Query | |
* | |
* @since 2.0.0 | |
*/ | |
function alm_query_posts($bypass = false) { | |
if(!$bypass){ | |
$nonce = $_GET['nonce']; | |
// Check our nonce, if they don't match then bounce! | |
if (! wp_verify_nonce( $nonce, 'ajax_load_more_nonce' )) | |
die('Get Bounced!'); | |
} | |
$repeater = (isset($_GET['repeater'])) ? $_GET['repeater'] : 'default'; | |
$postType = (isset($_GET['postType'])) ? $_GET['postType'] : 'post'; | |
$postFormat = (isset($_GET['postFormat'])) ? $_GET['postFormat'] : ''; | |
$category = (isset($_GET['category'])) ? $_GET['category'] : ''; | |
$author_id = (isset($_GET['author'])) ? $_GET['author'] : ''; | |
$meta_query = (isset($_GET['meta_query'])) ? $_GET['meta_query'] : ''; | |
$taxonomy = (isset($_GET['taxonomy'])) ? $_GET['taxonomy'] : ''; | |
$taxonomy_terms = (isset($_GET['taxonomy_terms'])) ? $_GET['taxonomy_terms'] : ''; | |
$taxonomy_operator = $_GET['taxonomy_operator']; | |
if($taxonomy_operator == ''){ | |
$taxonomy_operator = 'IN'; | |
} | |
$post_format = (isset($_GET['postFormat'])) ? $_GET['postFormat'] : ''; | |
$tag = (isset($_GET['tag'])) ? $_GET['tag'] : ''; | |
$s = (isset($_GET['search'])) ? $_GET['search'] : ''; | |
$order = (isset($_GET['order'])) ? $_GET['order'] : 'DESC'; | |
$orderby = (isset($_GET['orderby'])) ? $_GET['orderby'] : 'date'; | |
$exclude = (isset($_GET['exclude'])) ? $_GET['exclude'] : ''; | |
$numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 6; | |
$page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0; | |
$offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0; | |
$lang = (isset($_GET['lang'])) ? $_GET['lang'] : ''; | |
// Set up initial args | |
$args = array( | |
'post_type' => $postType, | |
'category_name' => $category, | |
'tag' => $tag, | |
'author' => $author_id, | |
'posts_per_page' => $numPosts, | |
'offset' => $offset + ($numPosts*$page), | |
's' => $s, | |
'order' => $order, | |
'orderby' => $orderby, | |
'lang' => $lang, | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => false, | |
'meta_query' => $meta_query | |
); | |
// Exclude posts if needed - See plugin examples for more info on excluding posts | |
if(!empty($exclude)){ | |
$exclude=explode(",",$exclude); | |
$args['post__not_in'] = $exclude; | |
} | |
// Post Format query | |
if(!empty($postFormat)){ | |
$format = "post-format-$postFormat"; | |
//If query is for standrd we need to filter by NOT IN | |
if($format == 'post-format-standard'){ | |
if (($post_formats = get_theme_support('post-formats')) && is_array($post_formats[0]) && count($post_formats[0])) { | |
$terms = array(); | |
foreach ($post_formats[0] as $format) { | |
$terms[] = 'post-format-'.$format; | |
} | |
} | |
$args['tax_query'] = array( | |
array( | |
'taxonomy' => 'post_format', | |
'terms' => $terms, | |
'field' => 'slug', | |
'operator' => 'NOT IN' | |
) | |
); | |
}else{ | |
$args['tax_query'] = array( | |
array( | |
'taxonomy' => 'post_format', | |
'field' => 'slug', | |
'terms' => array($format), | |
) | |
); | |
} | |
} | |
// Taxonomy query | |
if(!empty($taxonomy)){ | |
$the_terms = explode(", ", $taxonomy_terms); | |
$args['tax_query'] = array( | |
'relation' => 'OR', | |
array( | |
'taxonomy' => $taxonomy, | |
'field' => 'slug', | |
'terms' => $the_terms, | |
'operator' => $taxonomy_operator | |
), | |
); | |
} | |
// Meta query | |
if(!empty($meta_query)){ | |
$the_meta = wp_parse_args($meta_query); | |
$args['meta_query'] = array($the_meta); | |
} | |
// Query by $args | |
$alm_query = new WP_Query( $args ); | |
// the WP loop | |
if ($alm_query->have_posts()) : | |
while ($alm_query->have_posts()): $alm_query->the_post(); | |
$file = $repeater; | |
$include = ''; | |
$found = false; | |
if (has_action('alm_repeater_installed')){// If Custom Repeaters is installed | |
$repeaterLength = ALM_REPEATER_LENGTH; | |
if(!defined('ALM_REPEATER_LENGTH')){ | |
$repeaterLength = 6; | |
} | |
for ($i = 2; $i <= $repeaterLength + 2; $i++) { | |
$repeaterVal = 'repeater' . $i; | |
if($file == $repeaterVal){ | |
$include = ALM_REPEATER_PATH . 'repeaters/'. $file .'.php'; | |
//confirm file exists | |
if(!file_exists($include)){ | |
$include = plugin_dir_path( __FILE__ ) . 'core/repeater/default.php'; | |
} | |
$found = true; | |
} | |
} | |
if(!$found){ | |
$include = plugin_dir_path( __FILE__ ) . 'core/repeater/default.php'; | |
} | |
}else{ | |
$include = plugin_dir_path( __FILE__ ) . 'core/repeater/default.php'; | |
} | |
//Include repeater template | |
include( $include ); | |
endwhile; | |
endif; | |
wp_reset_query(); | |
exit; | |
} | |
} | |
/* | |
* AjaxLoadMore | |
* The main function responsible for returning the one true AjaxLoadMore Instance to functions everywhere. | |
* | |
* @since 2.0.0 | |
*/ | |
function AjaxLoadMore(){ | |
global $ajax_load_more; | |
if( !isset($ajax_load_more) ) | |
{ | |
$ajax_load_more = new AjaxLoadMore(); | |
} | |
return $ajax_load_more; | |
} | |
// initialize | |
AjaxLoadMore(); | |
endif; // class_exists check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php echo do_shortcode('[ajax_load_more repeater="repeater" post_type="project" meta_query="key=end_date&value='.date('Ymd').'&compare=>=" order="ASC" orderby="title" offset="6" posts_per_page="6" scroll="false" pause="true" button_label="Load more projects"]'); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment