Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active January 29, 2025 18:16
Show Gist options
  • Save xnau/7d4da6a4a0ca573d2a379549fe63ab0a to your computer and use it in GitHub Desktop.
Save xnau/7d4da6a4a0ca573d2a379549fe63ab0a to your computer and use it in GitHub Desktop.
Shows how to set up a field that holds a literal list of relations for the Participants Database Multi-Relational plugin
<?php
/**
* Plugin Name: PDb Multi-Relational Literal Relations Field
* Description: Maintains a field in the database with a list of the record's relations
* Version: 0.1
*/
/*
* this is designed to maintain a field in the record that can be used for simple filtering
*
* without this plugin and the field it maintains, it is not possible to filter
* based on a record's relations, for example to show a list of all the students
* for a particular teacher.
*
* to use this plugin you must have a text type field set up to recieve the names of the
* related records.
* The name of that field is assigned to the $fieldname variable
*/
class pdb_literal_relations_field {
/**
* @var string name of the literal relations field
*/
private $fieldname = 'teachers';
/**
* @var string name of the relation to mirror
*/
private $typename = 'teacher';
/**
*
*/
public function __construct()
{
add_action( 'pdb-after_submit_update', [ $this, 'update_literal_field'] );
}
/**
* updates the literal relations field in the record
*
* @param array $record the updated record data
*/
public function update_literal_field( $record )
{
/* this class must be available by having the
* PDb Multi-Relational Utility Class plugin activated
* https://gist.github.com/xnau/fb9cc360c58eba270c456dcdf3c6cb4c
*/
if ( !class_exists( 'pdbmrdb_record' ) )
{
return;
}
$record_relations = new pdbmrdb_record( $record['id'] );
$relations_list = $record_relations->get_relations( $this->typename );
$relations_titles = [];
foreach( $relations_list as $id => $title )
{
$relations_titles[] = $title;
}
$relations_string = implode( ', ', $relations_titles );
Participants_Db::write_participant( [ $this->fieldname => $relations_string ], $record['id']);
}
}
new pdb_literal_relations_field();
@xnau
Copy link
Author

xnau commented Jan 26, 2025

This is meant to be installed as a plugin: How to Install a WordPress Plugin from a Gist

You must also install and activate this plugin: PDb Multi-Relational Utility Class

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