Skip to content

Instantly share code, notes, and snippets.

@alispx
Created September 29, 2015 06:22
Show Gist options
  • Save alispx/94593eec6d8f69febd15 to your computer and use it in GitHub Desktop.
Save alispx/94593eec6d8f69febd15 to your computer and use it in GitHub Desktop.
<?php
/**
* Get all type posts
*
* @return void
* @author alispx
**/
function alispx_get_type_posts_data( $post_type = 'post' ) {
$posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => $post_type,
));
$result = array();
foreach ( $posts as $post ) {
$result[] = array(
'value' => $post->ID,
'label' => $post->post_title,
);
}
return $result;
}
add_action( 'vc_before_init', 'alispx_post_finder_integrateWithVC' );
function alispx_post_finder_integrateWithVC() {
vc_map( array(
'name' => esc_html__( 'Alispx Post Finder', 'alispx' ),
'base' => 'alispx_post_finder',
'class' => 'alispx-ico',
'icon' => 'alispx-ico',
'category' => esc_html__( 'Features', 'alispx' ),
'admin_enqueue_css' => array( get_template_directory_uri . '/your-path/alispxvc.css' ),
'description' => esc_html__( 'Alispx Post Finder', 'alispx' ),
'params' => array(
array(
'type' => 'autocomplete',
'class' => '',
'heading' => esc_html__( 'Post Name', 'alispx' ),
'param_name' => 'id',
'settings' => array( 'values' => alispx_get_type_posts_data() ),
),
)
) );
}
@carmelobaglieri
Copy link

Replace $tag with vc_map 'base' field and $param_name with autocomplete field param name

add_filter( "vc_autocomplete_{$tag}_{$param_name}_callback", function ( $query, $tag, $param_name ){
   
   $args = [
      'fields' => 'ids'
   ];
   
   if( $query ){
      $args['s'] = $query;
   }
   
   $data= [];
   
   foreach ( get_posts( $args ) as $id ) {
      $data[] = [
         'value' => $id,
         'label' => get_the_title( $id )
      ];
   }

   return $data;
  
}, 20, 3 );

add_filter( "vc_autocomplete_{$tag}_{$param_name}_render", function ( $value, $settings, $tag ){
   
   $value['label'] = get_the_title($value['value']);
   return $value;
  
}, 20, 3 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment