public
Created

Beastly AJAX SQL WordPress stuff

  • Download Gist
fml.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
<?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... -->

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.