|
<?php |
|
/* |
|
* Written by Tom Morton (Github: tmort) |
|
* To answer Nick83 Stackexchange question: https://wordpress.stackexchange.com/questions/283420/ |
|
*/ |
|
|
|
/* |
|
* Adding scripts to the theme. Note the wp_localize_script: This is how we provide ajax URL and security to the custom.js file. |
|
* |
|
* Sources and further reading: |
|
* https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
|
* https://codex.wordpress.org/Function_Reference/wp_localize_script |
|
* https://www.barrykooij.com/check-nonces-wordpress-ajax-calls/ |
|
* https://codex.wordpress.org/Function_Reference/wp_verify_nonce |
|
*/ |
|
add_action( 'wp_enqueue_scripts', 'nick83_enqueue' ); |
|
function nick83_enqueue() { |
|
wp_enqueue_script( 'customjs', get_stylesheet_directory_uri() . '/custom.js', array( 'jquery' ), '1.0', true ); |
|
//nklocal |
|
wp_localize_script( 'customjs', 'nklocal', array( |
|
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
'security' => wp_create_nonce( 'nick83_security_key' ), |
|
) ); |
|
} |
|
|
|
/* |
|
* This is your first ajax call. I have two actions because WordPress allows non-logged-in users to call ajax via "wp_ajax_nopriv" |
|
* We are also checking a nonce value (provided by wp_localize_script and custom.js) |
|
* |
|
* Sources: |
|
* https://codex.wordpress.org/AJAX_in_Plugins |
|
* https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) |
|
* https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action) |
|
*/ |
|
add_action( 'wp_ajax_nopriv_nick83_get_posts_with_meta', 'nick83_get_posts_with_meta' ); |
|
add_action( 'wp_ajax_nick83_get_posts_with_meta', 'nick83_get_posts_with_meta' ); |
|
function nick83_get_posts_with_meta() { |
|
$data = esc_sql( $_POST ); |
|
if ( ! wp_verify_nonce( $data['security'], 'nick83_security_key' ) ) { |
|
wp_die( 'Security check' ); |
|
} |
|
|
|
$options = nick83_query_meta_posts(); |
|
|
|
if ( ! empty( $options ) ) { |
|
echo json_encode( array( 'msg' => 'success', 'data' => $options ) ); |
|
} |
|
|
|
wp_die(); |
|
} |
|
|
|
/* |
|
* For a clean functions.php, I have broken the AJAX function and the query into two separate functions. This function contains the query and method we used previously to remove duplicates. Once complete, it returns the non-duplicate array of prices. |
|
*/ |
|
function nick83_query_meta_posts() { |
|
$args = array( |
|
'post_type' => 'post', //define the post type you want |
|
'posts_per_page' => '-1', |
|
'meta_key' => 'prix_1', |
|
); |
|
|
|
$price_query = new WP_Query( $args ); |
|
|
|
$options = array(); |
|
|
|
if ( $price_query->have_posts() ) { |
|
while ( $price_query->have_posts() ) { |
|
$price_query->the_post(); |
|
$field = get_field( 'prix_1' ); |
|
|
|
//Here we make sure there are no duplicates |
|
if ( ! in_array( $field, $options ) ) { |
|
$options[] = $field; |
|
} |
|
} |
|
} |
|
|
|
return $options; |
|
} |
|
|
|
|
|
/* |
|
* This is the second ajax call. In custom.js, we grab the price the user has chosen and include it in the $_POST (which is turned into $data). If $data['price'] exists, we can give it to the function 'nick83_query_meta_posts_with_price' and hopefully have posts returned. |
|
*/ |
|
add_action( 'wp_ajax_nopriv_nick83_render_list_of_posts', 'nick83_render_list_of_posts' ); |
|
add_action( 'wp_ajax_nick83_render_list_of_posts', 'nick83_render_list_of_posts' ); |
|
function nick83_render_list_of_posts() { |
|
$data = esc_sql( $_POST ); |
|
if ( ! wp_verify_nonce( $data['security'], 'nick83_security_key' ) ) { |
|
wp_die( 'Security check' ); |
|
} |
|
|
|
if ( ! isset( $data['price'] ) || empty( $data['price'] ) ) { |
|
wp_die( 'No Price' ); |
|
} |
|
|
|
|
|
$options = nick83_query_meta_posts_with_price( $data['price'] ); |
|
if ( ! empty( $options ) ) { |
|
echo json_encode( array( 'msg' => 'success', 'data' => $options ) ); |
|
} |
|
|
|
wp_die(); |
|
} |
|
|
|
/* |
|
* This is the function that takes a price ($data['price'] from the ajax function) and uses it in a WordPress query to see if we can find a meta_key of 'prix_1' and a meta value of the price chosen. |
|
* |
|
* Note here that I am creating an empty array, $links, and then adding the title of the post as the array key and the URL of the post as the array value. |
|
*/ |
|
function nick83_query_meta_posts_with_price( $price = false ) { |
|
if ( $price ) { |
|
$args = array( |
|
'post_type' => 'post', //define the post type you want |
|
'posts_per_page' => '-1', |
|
'meta_key' => 'prix_1', |
|
'meta_value' => $price, |
|
); |
|
|
|
$links = array(); |
|
$meta_query = new WP_Query( $args ); |
|
if ( $meta_query->have_posts() ) { |
|
while ( $meta_query->have_posts() ) { |
|
$meta_query->the_post(); |
|
|
|
//Array[key] = value; |
|
$links[ get_the_title() ] = get_permalink(); |
|
} |
|
} |
|
|
|
if ( ! empty( $links ) ) { |
|
return $links; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
/* |
|
* *** WARNING *** |
|
* I am only using this piece of code because I do not have ACF installed. |
|
* If you have ACF you will not need this. |
|
*/ |
|
if ( ! function_exists( 'get_field' ) ) { |
|
function get_field( $key ) { |
|
global $post; |
|
|
|
return get_post_meta( $post->ID, $key, true ); |
|
} |
|
} |