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() ),
),
)
) );
}
@alispx
Copy link
Author

alispx commented Jun 30, 2016

Hi,

I think it does not. Because it's use autocomplete field.

@dinhtungdu
Copy link

dinhtungdu commented Jul 5, 2016

I agree with @EmranAhmed, because WP still needs to query all of your posts and prepares an array for all posts. I stuck in the same situation with redux framework. Using -1 for preload all posts, and the admin goes crazy slow when customer has about 10k posts.

@bene82
Copy link

bene82 commented Oct 7, 2016

Great. It's what I was searching for! Thanks a lot.

@cayaumas
Copy link

How i can make a custom ajax call on the function alispx_get_type_posts_data, i want to get the value of the input box on this function on real time to get new values to suggest, is that possible?

@arifulislam121
Copy link

How are you? hope you are well.
I see you visual composer autocomplete param type code on GitHub .this is fantastic working in my wp environment backend but how can display the selected value in the frontend. how can custom query for getting This value .can you help, please 🙏 . I will wait for your reply. advance Thanks

@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