Guest User

Untitled

a guest
Feb 12th, 2018
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
  3.     $prefix       = '';
  4.     $meta_boxes[] = array(
  5.         'id'     => 'finite_scroll',
  6.         'title'  => __( 'Finite Scroll' ),
  7. //      'post_types' => array( 'recipe' ),
  8.         'fields' => array(
  9.             array(
  10.                 'id'         => $prefix . 'related_recipe',
  11.                 'type'       => 'group',
  12.                 'clone'      => true,
  13.                 'sort_clone' => true,
  14.                 'fields'     => array(
  15.                     array(
  16.                         'name'       => __( 'Related Recipe:' ),
  17.                         'id'         => $prefix . 'rel_recipe',
  18.                         'type'       => 'select_advanced',
  19.                         'options'    => prefix_get_selected_post(),
  20.                         'js_options' => array(
  21.                             'ajax'               => array(
  22.                                 'url'      => '/wp-admin/admin-ajax.php?action=select_advanced_post_lookup&post_type=post',
  23.                                 'dataType' => 'json',
  24.                                 'type'     => 'get',
  25.                                 'delay'    => '250',
  26.                             ),
  27.                             'minimumInputLength' => 3,
  28.                             'allowClear'         => true,
  29.                             'placeholder'        => 'Select recipe',
  30.                         ),
  31.                     ),
  32.                 ),
  33.             ),
  34.         ),
  35.     );
  36.  
  37.     return $meta_boxes;
  38. } );
  39.  
  40. add_action( 'wp_ajax_select_advanced_post_lookup', 'select_advanced_post_lookup' );
  41. function select_advanced_post_lookup() {
  42.     global $wpdb;
  43.     $post_types = "'" . implode( "','", explode( ',', $_GET['post_type'] ) ) . "'";
  44.     $keyword    = "%" . $wpdb->esc_like( $_GET['q'] ) . "%";
  45.  
  46.     $sql      = "SELECT ID, post_title" .
  47.                 " FROM $wpdb->posts" .
  48.                 " WHERE post_title LIKE '" . $keyword . "'" .
  49.                 " AND post_type IN (" . $post_types . ")" .
  50.                 " AND post_status='publish' " .
  51.                 " ORDER BY post_title LIMIT 0,15;";
  52.     $results  = $wpdb->get_results( $sql );
  53.     $response = array(
  54.         'results' => array(),
  55.     );
  56.     foreach ( $results as $result ) {
  57.         $response['results'][] = array( 'id' => $result->ID, 'text' => $result->post_title );
  58.     }
  59.     wp_send_json( $response );
  60. }
  61.  
  62.  
  63. /**
  64.  * Get current object ID.
  65.  *
  66.  * @return int
  67.  */
  68. function prefix_get_current_post_id() {
  69.     $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
  70.     if ( ! $post_id ) {
  71.         $post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
  72.     }
  73.  
  74.     return is_numeric( $post_id ) ? absint( $post_id ) : false;
  75. }
  76.  
  77. function prefix_get_selected_post() {
  78.     $default = array( '' => '' );
  79.     $post_id = prefix_get_current_post_id();
  80.     if ( ! $post_id ) {
  81.         return $default;
  82.     }
  83.     $group = get_post_meta( $post_id, 'related_recipe', true );
  84.     if ( empty( $group ) ) {
  85.         return $default;
  86.     }
  87.     $options = array();
  88.     foreach ( $group as $clone ) {
  89.         if ( ! isset( $clone['rel_recipe'] ) ) {
  90.             continue;
  91.         }
  92.         $id             = $clone['rel_recipe'];
  93.         $options[ $id ] = get_the_title( $id );
  94.     }
  95.  
  96.     return $options ? $options : $default;
  97. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×