Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active September 19, 2024 15:05
Show Gist options
  • Save Crocoblock/b6aad0ff390c04728a1c314a18476017 to your computer and use it in GitHub Desktop.
Save Crocoblock/b6aad0ff390c04728a1c314a18476017 to your computer and use it in GitHub Desktop.
JetEngine Get query by ID, get items from query

Call these methods not earlier than on 'init' with the priority of 12, queries are not registered before that

Get query list

Returns an array of $id => $name

$queries = \Jet_Engine\Query_Builder\Manager::instance()->get_queries_for_options()

Get query by ID

ID is an integer, can be found in query URL in Query Builder;
not to be mistaken with Query ID parameter which is used for JetSmartFilters

$query = \Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id( $query_id );

Get query items

Returns an array of objects

$query_items = $query->get_items();

Filter query items before output

$items is an array of objects
$query is an object of class which is an extension of
\Jet_Engine\Query_Builder\Queries\Base_Query

$query->id property is the ID of the query
$query->name is a name of query

see \jet-engine\includes\components\query-builder\queries\base.php for the properties / methods

add_filter( 'jet-engine/query-builder/query/items', function( $items, $query ) {
  
  //if query ID is 128 or query name contains '--filter-result' substring - return post with ID of 256
  if ( $query->id == 128 || false !== strpos( $query->name, '--filter-result' ) ) {
    $items = array( get_post( 256 ) );
  }
  
  return $items;
  
}, 0, 2 );

Change query parameters after setup

add_action( 'jet-engine/query-builder/query/after-query-setup', function( $query ) {
    
    if ( false === strpos( $query->name ?? '', '--current-record' ) ) {
        return;
    }
    
    $object = jet_engine()->listings->data->get_current_object();
    
    if ( empty( $object->id ) ) {
        return;
    }
    
    $query->final_query['record__in'] = array( $object->id );
    
} );
@nextab
Copy link

nextab commented Apr 2, 2024

In the last section, it always returns after

if ( empty( $object->id ) ) {
        return;
    }

Cause "ID" should be higher case.

While I appreciate the code snippets, I wanted to have an option to click on a button and update the listing grid accordingly. I still don't know how to accomplish that. :-(

@ihslimn
Copy link

ihslimn commented Apr 3, 2024

there is no typo :)
for that particular case that was used in my tests, current object had an id property

as for your question abnout updating the listing - you may refer to this https://gist.github.com/ihslimn/052a27bb63d2852ed5de74193dd6667d

https://gist.github.com/ihslimn/052a27bb63d2852ed5de74193dd6667d#file-update-listing-on-variation-change-html-L32-L37 this piece of code sends an ajax request to get listing content and then displays the result

@estevan-ulian
Copy link

Hi guys! I have a query created using the Related Items By Sibling Relation¹ feature in the QueryBuilder, where the Direct Object ID From parameter is a query variable.

How do I query/filter results dynamically through my $query_variable?

Example:

function get_result_items( $query_variable )
    {
        $query_id = 123; // my query id from query builder
        $query = \Jet_Engine\Query_Builder\Manager::instance()->get_query_by_id($query_id);

        $result_items = array(); 

        // do something to get the results filtered by $query_variable

        return $result_items;
    }

¹ Related Items By Sibling Relation: https://gist.github.com/Crocoblock/6645115b812a58fc850eaa883e0505f2

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