Created
September 29, 2015 06:22
-
-
Save alispx/94593eec6d8f69febd15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() ), | |
), | |
) | |
) ); | |
} |
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
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