Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
- $prefix = '';
- $meta_boxes[] = array(
- 'id' => 'finite_scroll',
- 'title' => __( 'Finite Scroll' ),
- // 'post_types' => array( 'recipe' ),
- 'fields' => array(
- array(
- 'id' => $prefix . 'related_recipe',
- 'type' => 'group',
- 'clone' => true,
- 'sort_clone' => true,
- 'fields' => array(
- array(
- 'name' => __( 'Related Recipe:' ),
- 'id' => $prefix . 'rel_recipe',
- 'type' => 'select_advanced',
- 'options' => prefix_get_selected_post(),
- 'js_options' => array(
- 'ajax' => array(
- 'url' => '/wp-admin/admin-ajax.php?action=select_advanced_post_lookup&post_type=post',
- 'dataType' => 'json',
- 'type' => 'get',
- 'delay' => '250',
- ),
- 'minimumInputLength' => 3,
- 'allowClear' => true,
- 'placeholder' => 'Select recipe',
- ),
- ),
- ),
- ),
- ),
- );
- return $meta_boxes;
- } );
- add_action( 'wp_ajax_select_advanced_post_lookup', 'select_advanced_post_lookup' );
- function select_advanced_post_lookup() {
- global $wpdb;
- $post_types = "'" . implode( "','", explode( ',', $_GET['post_type'] ) ) . "'";
- $keyword = "%" . $wpdb->esc_like( $_GET['q'] ) . "%";
- $sql = "SELECT ID, post_title" .
- " FROM $wpdb->posts" .
- " WHERE post_title LIKE '" . $keyword . "'" .
- " AND post_type IN (" . $post_types . ")" .
- " AND post_status='publish' " .
- " ORDER BY post_title LIMIT 0,15;";
- $results = $wpdb->get_results( $sql );
- $response = array(
- 'results' => array(),
- );
- foreach ( $results as $result ) {
- $response['results'][] = array( 'id' => $result->ID, 'text' => $result->post_title );
- }
- wp_send_json( $response );
- }
- /**
- * Get current object ID.
- *
- * @return int
- */
- function prefix_get_current_post_id() {
- $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
- if ( ! $post_id ) {
- $post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );
- }
- return is_numeric( $post_id ) ? absint( $post_id ) : false;
- }
- function prefix_get_selected_post() {
- $default = array( '' => '' );
- $post_id = prefix_get_current_post_id();
- if ( ! $post_id ) {
- return $default;
- }
- $group = get_post_meta( $post_id, 'related_recipe', true );
- if ( empty( $group ) ) {
- return $default;
- }
- $options = array();
- foreach ( $group as $clone ) {
- if ( ! isset( $clone['rel_recipe'] ) ) {
- continue;
- }
- $id = $clone['rel_recipe'];
- $options[ $id ] = get_the_title( $id );
- }
- return $options ? $options : $default;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement