Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created November 28, 2024 12:33
Show Gist options
  • Save MjHead/284f43a3b944765c75c491d20e08593f to your computer and use it in GitHub Desktop.
Save MjHead/284f43a3b944765c75c491d20e08593f to your computer and use it in GitHub Desktop.
JetEngine. Example of custom context to get some object by ID of the object provided in the query
<?php
/**
* Slug of the context.
* You can change it but please kepp a snake-case namin and latin letters
*/
$custom_context_slug = 'custom-object-by-id';
/**
* Register context for the context dropdown
*/
add_filter( 'jet-engine/listings/allowed-context-list', function() use ( $custom_context_slug ) {
// here you can change 'Custom Object by ID' in the way you need
$context_list[ $custom_context_slug ] = 'Custom Object by ID';
return $context_list;
} );
/**
* Get the object for the you custom context.
*/
add_filter( 'jet-engine/listings/data/object-by-context/' . $custom_context_slug, function() use ( $custom_context_slug ) {
$query_var = 'ID'; // Here you can set query var you need instead of 'ID'
$id = ! empty( $_REQUEST[ $query_var ] ) ? absint( $_REQUEST[ $query_var ] ) : false;
if ( ! $id ) {
return false;
}
/**
* This is example for the post, but you can replace this part with getting any object you need.
*/
$object = get_post( $id );
if ( ! $object ) {
return false;
}
return $object;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment