Skip to content

Instantly share code, notes, and snippets.

@LaxusCroco
Last active April 7, 2023 07:19
Show Gist options
  • Save LaxusCroco/c9e015239ee266c00bce314886fdee75 to your computer and use it in GitHub Desktop.
Save LaxusCroco/c9e015239ee266c00bce314886fdee75 to your computer and use it in GitHub Desktop.
Items with Relation macro
<?php
add_action( 'jet-engine/register-macros', function(){
class Items_With_Relation extends \Jet_Engine_Base_Macros {
public function macros_tag() {
return 'items_with_relation';
}
public function macros_name() {
return esc_html__( 'Items with Relation', 'jet-engine' );
}
public function macros_args() {
return array(
'rel_id' => array(
'label' => __( 'From Relation', 'jet-engine' ),
'type' => 'select',
'options' => function() {
return jet_engine()->relations->get_relations_for_js( true, __( 'Select...', 'jet-engine' ) );
},
'default' => '',
),
'rel_object' => array(
'label' => __( 'From Object (what to show)', 'jet-engine' ),
'type' => 'select',
'options' => array(
'parent_object_id' => 'Parents with children',
'child_object_id' => 'Children with parents',
),
'default' => 'parent_object_id',
),
);
}
public function macros_callback( $args = array() ) {
$relation_id = $args['rel_id'];
if ( ! $relation_id ) {
return '';
}
$relation_object = jet_engine()->relations->get_active_relations( $relation_id );
if ( ! $relation_object ) {
return '';
}
$db_table = $relation_object->get_args( 'db_table' );
$object_type = $args['rel_object'];
global $wpdb;
$prefix = $wpdb->prefix;
if ( ! $db_table ) {
$results = $wpdb->get_results( "SELECT * FROM {$prefix}jet_rel_default AS jet_rel_default WHERE rel_id = '$relation_id' GROUP BY $object_type;", ARRAY_A );
} else {
$results = $wpdb->get_results( "SELECT * FROM {$prefix}jet_rel_$relation_id WHERE rel_id = '$relation_id' GROUP BY $object_type;", ARRAY_A );
}
$ids = array_column( $results, $object_type );
return implode( ',', $ids );
}
}
new Items_With_Relation();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment