Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ihslimn/aeb76a778ed44d32faf640f4d3bda8ea to your computer and use it in GitHub Desktop.
Save ihslimn/aeb76a778ed44d32faf640f4d3bda8ea to your computer and use it in GitHub Desktop.
Get properties from query macro
//add Get properties from query macro
add_filter( 'jet-engine/listings/macros-list', 'register_query_items_macro' );
function register_query_items_macro( $macros_list ) {
$macros_list['get_props_from_query'] = array(
'label' => 'Get properties from query',
'cb' => 'get_props_from_query_macro',
'args' => array(
'query_id' => array(
'label' => 'Query ID',
'type' => 'select',
'options' => Jet_Engine\Query_Builder\Manager::instance()->get_queries_for_options(),
),
'property' => array(
'label' => 'Property',
'type' => 'text',
'default' => '',
),
),
);
return $macros_list;
}
function get_props_from_query_macro( $field_value = null, $args = null ) {
$args = explode( '|', $args );
$query_id = $args[0];
$property = $args[1];
if ( ! $query_id || ! $property ) {
return;
}
$query = Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id( $query_id );
$result = 'not-found';
if ( $query ) {
$query_items = $query->get_items();
if ( ! empty( $query_items ) ) {
$properties_array = array_column( $query_items, $property );
$result = array_unique( $properties_array );
$result = implode( ',', $result );
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment