Loop Carousel with Related Items
-
Hello. I’ve got two related Pods (locations, tokens) that I’d like to serve up content from in an Elementor Pro Loop Carousel widget.
I’ve proven that the queries I’m using are working just fine, as you can see with the shortcode implementation directly below the carousel. But it appears that Elementor isn’t honoring the query hooks I’m using:
add_action('elementor/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);add_action('elementor_pro/posts/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);add_action('elementor_pro/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);Elementor Pro Support told me they couldn’t help me, so I’m posting here in the event someone has encountered this issue and surmounted it. It would seem a fairly common use case.
I know that the first query hook DID work at some point, but when I upgraded Elementor Pro recently, it stopped working. This old post also suggests my first query hook should work. But the widget is rendering no content when ‘locations_by_token’ is entered as the query.
Here’s my code from functions.php, for reference:
/** TOKEN page → Related LOCATIONS (field_id=86669) */
function aa_query_locations_by_token($query){
global $wpdb;
// Resolve token ID even in AJAX preview
$token_id = get_queried_object_id();
if (!$token_id && !empty($GLOBALS['post']->ID)) $token_id = (int) $GLOBALS['post']->ID;
$ids = $token_id ? $wpdb->get_col($wpdb->prepare(
"SELECT pr.related_item_id
FROM {$wpdb->prefix}podsrel pr
WHERE pr.field_id = %d AND pr.item_id = %d
ORDER BY pr.weight ASC", 86669, $token_id
)) : [];
$ids = array_map('intval', array_unique($ids));
// Defensive: filter to published oum-location only
if ($ids) {
$placeholders = implode(',', array_fill(0, count($ids), '%d'));
$rows = $wpdb->get_col($wpdb->prepare(
"SELECT ID FROM {$wpdb->posts}
WHERE ID IN ($placeholders)
AND post_type = 'oum-location'
AND post_status = 'publish'",
...$ids
));
$ids = array_map('intval', $rows);
}
// Hard override & clear conflicts
$query->set('post_type', 'oum-location'); // slug with dash
$query->set('post_status', 'publish');
$query->set('post__in', $ids ?: [0]); // [0] -> empty result
$query->set('orderby', 'post__in');
$query->set('posts_per_page', max(1, min(50, count($ids) ?: 12))); // finite count
$query->set('paged', 1);
$query->set('ignore_sticky_posts', true);
$query->set('no_found_rows', true);
// scrub anything Elementor (or another plugin) added
$query->set('tax_query', []);
$query->set('meta_query', []);
$query->set('author', '');
$query->set('s', '');
$query->set('offset', 0);
// Dump final args actually sent to WP_Query
$dump = [
'hook' => 'locations_by_token',
'token' => $token_id,
'vars' => array_intersect_key($query->query_vars, array_flip([
'post_type','post_status','post__in','orderby','posts_per_page','paged','tax_query','meta_query','author','s','offset'
])),
];
aa_emit_footer_note('FINAL ' . json_encode($dump));
}
add_action('elementor/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);
add_action('elementor_pro/posts/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);
add_action('elementor_pro/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);The page I need help with: [log in to see the link]
The topic ‘Loop Carousel with Related Items’ is closed to new replies.