Created
June 18, 2012 20:01
-
-
Save zachlysobey/2950395 to your computer and use it in GitHub Desktop.
Beastly AJAX SQL WordPress stuff
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 | |
////////////////////////////////////////////////////////////// | |
// Ajax Project | |
///////////////////////////////////////////////////////////// | |
add_action( 'wp_ajax_nopriv_myajax-submit', 'ajax_project_load' ); | |
add_action( 'wp_ajax_myajax-submit', 'ajax_project_load' ); | |
function ajax_project_load() { | |
$the_slug = $_POST['slug']; | |
$args=array( | |
'name' => $the_slug, | |
'post_type' => 'projects', | |
'post_status' => 'publish', | |
'showposts' => 1, | |
'ignore_sticky_posts' => 1 | |
); | |
$my_posts = get_posts($args); | |
if( $my_posts ) : | |
global $post, $postCapabilityFilter, $postFocusFilter; | |
$post = $my_posts[0]; | |
$postCapabilityFilter = $_POST['capFilter']=='undefined'? '' : substr($_POST['capFilter'], 1); | |
$postCapabilityFilter = ereg_replace("[^A-Za-z0-9\-]", "", $postCapabilityFilter ); | |
$postFocusFilter = $_POST['focFilter']=='undefined'? '' : substr($_POST['focFilter'], 1); | |
$postFocusFilter = ereg_replace("[^A-Za-z0-9\-]", "", $postFocusFilter ); | |
$response = json_encode( "Success" ); | |
header( "Content-Type: application/json" ); | |
?> | |
<div id="ajax-project-<?php the_ID(); ?>" <?php post_class('project main ajax clearfix'); ?> > | |
<div class="projectHeader"> | |
<div class="projectNav clearfix"> | |
<?php | |
function filtered_adjacent_post($prev_next){ | |
global $wpdb, $post, $postCapabilityFilter, $postFocusFilter; | |
$the_ID = $post->ID; | |
$querystr = " | |
SELECT $wpdb->postmeta.*, $wpdb->posts.post_name | |
FROM $wpdb->posts, $wpdb->postmeta, $wpdb->terms, $wpdb->term_relationships | |
WHERE $wpdb->posts.post_type = 'projects' | |
AND $wpdb->posts.post_status = 'publish' | |
AND $wpdb->posts.ID "; | |
$querystr .= $prev_next=='next'? "< $the_ID" : "> $the_ID"; | |
$querystr .= "\n" . "AND $wpdb->posts.ID = $wpdb->postmeta.post_id"; | |
if ($postCapabilityFilter != ''){ // Capability Filter Set | |
$querystr .= "\n" . "AND $wpdb->postmeta.meta_value LIKE '%" . $postCapabilityFilter . "%'"; | |
}elseif ($postFocusFilter != ''){ // Focus Filter Set | |
$querystr .= "\n" . "AND $wpdb->term_relationships.object_id = $wpdb->posts.ID"; | |
$querystr .= "\n" . "AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id"; | |
$querystr .= "\n" . "AND $wpdb->terms.slug = '$postFocusFilter'"; | |
} | |
$querystr .= "\n" . "ORDER BY $wpdb->posts.ID DESC"; | |
$nextProjQuery = $wpdb->get_results($querystr, OBJECT); | |
//print_r( $nextProjQuery); | |
//echo $querystr; | |
if (!$nextProjQuery){ // Loop if no more projects | |
$querystr = " | |
SELECT wp_postmeta.*, wp_posts.post_name | |
FROM $wpdb->posts, $wpdb->postmeta, $wpdb->terms, $wpdb->term_relationships | |
WHERE wp_posts.post_type = 'projects' | |
AND wp_posts.post_status = 'publish' | |
AND wp_posts.ID = wp_postmeta.post_id"; | |
if ($postCapabilityFilter != ''){ // Capability Filter Set | |
$querystr .= "\n" . "AND $wpdb->postmeta.meta_value LIKE '%" . $postCapabilityFilter . "%'"; | |
}elseif ($postFocusFilter != ''){ // Focus Filter Set | |
$querystr .= "\n" . "AND $wpdb->term_relationships.object_id = $wpdb->posts.ID"; | |
$querystr .= "\n" . "AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id"; | |
$querystr .= "\n" . "AND $wpdb->terms.slug = '$postFocusFilter'"; | |
} | |
$querystr .= "\n" . "ORDER BY wp_posts.ID "; | |
if($prev_next == 'next') { | |
$querystr .= "DESC"; | |
}else{ | |
$querystr .= "ASC"; | |
} | |
$nextProjQuery = $wpdb->get_results($querystr, OBJECT); | |
} | |
$nextProject = $nextProjQuery[0]; | |
$next_slug = $nextProject->post_name; | |
return $next_slug; | |
} | |
$next_slug = filtered_adjacent_post('next'); | |
$prev_slug = filtered_adjacent_post('prev'); | |
$nextPrevFilter = ''; | |
if ($postCapabilityFilter != ''){ | |
$nextPrevFilter .= "_$postCapabilityFilter"; | |
}elseif ($postFocusFilter != ''){ | |
$nextPrevFilter .= "_$postFocusFilter"; | |
} | |
?> | |
<div class="next <?php if(!$next_slug) echo 'inactive';?>"> | |
<?php if($prev_slug !='') : ?> | |
<a href="#<?php echo $prev_slug . $nextPrevFilter ;?>" onClick="nextPrevProject('<?php echo $prev_slug; ?>');">Previous</a> | |
<?php endif; ?> | |
</div> | |
<div class="previous <?php if(!$prev_slug) echo 'inactive';?>"> | |
<?php if($next_slug != '') : ?> | |
<a href="#<?php echo $next_slug . $nextPrevFilter ;?>" onClick="nextPrevProject('<?php echo $next_slug; ?>');">Next</a> | |
<?php endif; ?> | |
</div> | |
<div class="closeBtn"> | |
<a href="#">Close Project</a> | |
</div> | |
</div> | |
</div> | |
<!-- ...more stuff below... --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment