Created
February 10, 2018 19:14
-
-
Save reazuliqbal/21ea964f8ea7c62093827804ed5b0672 to your computer and use it in GitHub Desktop.
Querying WordPress serialized ACF meta data using WP_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
add_action( 'rest_api_init', function () { | |
// Path to meta query route e.g. localhost:3000/wp-json/reazul/v1/daily_post?for_day=10&for_month=3 | |
register_rest_route( 'reazul/v1', '/daily_post', array( | |
'methods' => 'GET', // API request method | |
'callback' => 'get_daily_post', // Call back to process request | |
'args' => array( // Arguments passed on to the callback and their validations | |
'for_day' => array( | |
'required' => true, | |
'validate_callback' => function($param, $request, $key) { | |
return is_numeric( $param ); // Checking for if the value is a number e.g. 1-31 | |
} | |
), | |
'for_month' => array( | |
'required' => true, | |
'validate_callback' => function($param, $request, $key) { | |
return is_numeric( $param ); // Checking for if the value is a number e.g. 1-12 | |
} | |
), | |
) | |
) ); | |
}); | |
// Callback function that returns API contents | |
function get_daily_devotional( WP_REST_Request $request ) { | |
$args = array( | |
'numberposts' => 1, | |
'post_type' => 'post', | |
'meta_query' => array( | |
'relation' => 'AND', // Relationship between two variables passed onto callback | |
array( | |
'key' => 'date', // ACF field key | |
'value' => "".$request->get_param( 'for_day' )."", // Getting query value from request. Additional Quote is used for turning it to string. | |
'compare' => 'LIKE', | |
), | |
array( | |
'key' => 'month', // ACF field key | |
'value' => "".$request->get_param( 'for_month' )."", // Getting query value from request. Additional Quote is used for converting it to string. | |
'compare' => 'LIKE', | |
), | |
) | |
); | |
$the_query = new WP_Query( $args ); | |
// Regular post loop | |
if( $the_query->have_posts() ) { | |
$data = array(); | |
while($the_query->have_posts()) { | |
$the_query->the_post(); | |
// Generating custom API contents | |
$data['ID'] = get_the_ID(); | |
$data['title'] = get_the_title(); | |
$data['permalink'] = get_the_permalink(); | |
$data['excerpt'] = wp_trim_words(get_the_content(), 75); | |
$data['content'] = get_the_content(); | |
} | |
return $data; | |
} else { | |
return new WP_Error( 'no_post', 'No post to show.', array( 'status' => 404 ) ); | |
} | |
} |
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
jQuery(document).ready(function($) { | |
$('form#dailyPost').submit(function(event) { | |
event.preventDefault(); | |
// Getting input from two dropdown | |
var month = $('#month').val(); | |
var day = $('#day').val(); | |
// Formating human readable date for the selected day and month in current year | |
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; | |
var current = new Date(); | |
var date = new Date(current.getFullYear(), (month - 1), day).toLocaleDateString("en-US",options); | |
$.ajax({ | |
url: '/wp-json/reazul/v1/daily_post', | |
dataType: 'json', | |
data: {for_day: day, for_month: month}, | |
}) | |
.done(function(e) { | |
$('.dailypost').html('<a href="'+ e.permalink +'" class="title">'+ e.title +'</a><p class="text-italic">Daily Post for '+ date +'</p><div class="post-content"><p>'+ e.excerpt +'</p><a href="'+ e.permalink +'" class="btn btn-primary">Continue reading <span class="fas fa-caret-right fa-xs fa-fw"></a></div>'); | |
}) | |
.fail(function() { | |
console.log("error"); | |
}); | |
}); | |
}); | |
// This function will run on load to show appropriate post for today's date | |
function getDailyPost() { | |
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; | |
var current = new Date(); | |
var month = current.getMonth() + 1; | |
var day = current.getDate(); | |
var date = new Date(current.getFullYear(), (month - 1), day).toLocaleDateString("en-US",options); | |
jQuery.ajax({ | |
url: '/wp-json/reazul/v1/daily_post', | |
dataType: 'json', | |
data: {for_day: day, for_month: month}, | |
}) | |
.done(function(e) { | |
jQuery('.dailypost').html('<a href="'+ e.permalink +'" class="title">'+ e.title +'</a><p class="text-italic">Daily Post for '+ date +'</p><div class="post-content"><p>'+ e.excerpt +'</p><a href="'+ e.permalink +'" class="btn btn-primary">Continue reading <span class="fas fa-caret-right fa-xs fa-fw"></a></div>'); | |
}) | |
.fail(function() { | |
console.log("error"); | |
}); | |
} | |
// Calling the function on page load | |
if (window.addEventListener) | |
window.addEventListener("load", getDailyDevotional, false); | |
else if (window.attachEvent) | |
window.attachEvent("onload", getDailyDevotional); | |
else window.onload = getDailyDevotional; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment