Last active
October 10, 2023 14:14
-
-
Save ellegaarddk/21a7097b3e5fb5f0fc368da388774e45 to your computer and use it in GitHub Desktop.
Custom queries for Elementor use - WordPress. To be inserted in you theme's functions.php or a custom made functions plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<php | |
/* | |
* Custom queries for Elementor | |
* by ellegaard ID | |
*/ | |
/* Get direct children of current page | |
* | |
* for query ID use 'pages_direct_child_filter' | |
*/ | |
add_action( 'elementor/query/pages_direct_child_filter', function( $query ) { | |
// Get current page | |
$current_page = get_queried_object_id(); | |
// Modify the query | |
$query->set( 'post_parent', $current_page ); | |
} ); | |
/* | |
* Get siblings of current page | |
* | |
* for query ID use 'pages_siblings_filter' | |
*/ | |
add_action( 'elementor/query/pages_siblings_filter', function( $query ) { | |
//get parent ID | |
$id = wp_get_post_parent_id( get_the_ID() ); | |
//modify query | |
$query->set( 'post_parent', $id ); | |
} ); | |
/* | |
* Getting same costum taxonomy of current page | |
* | |
* for query ID use 'pages_same_cpt_cat_filter' | |
*/ | |
add_action( 'elementor/query/pages_same_cpt_cat_filter', function( $query ) { | |
// get ID of current page | |
$id = get_queried_object_id(); | |
// tax terms of chosen taxonomy - insert instead of 'custom_taxonomy_name' | |
$tax = get_the_terms( $id, 'custom_taxonomy_name'); | |
// choose slug as field | |
$query->set( 'field', 'slug'); | |
// get slug of current tax | |
$query->set( 'tax_query', $tax[0]->slug ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment