• Resolved vindic8r

    (@vindic8r)


    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]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter vindic8r

    (@vindic8r)

    Plugin Support pdclark

    (@pdclark)

    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.

    I see there is content under a heading saying “Shortcode” in the linked page, but it is not clear how that is related, because a shortcode does not override a WP_Query like these filters, and the shortcode code is not in this post for reference.

    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);

    It’s not clear whether all these filters are necessary or not. Focus on one and verify it is running at all. According to the linked documentation, it should be the first, and locations_by_token must be specified in the Query ID field of the module you are attempting to filter, as shown in the screenshot at https://developers.elementor.com/docs/hooks/custom-query-filter/

    I know that the first query hook DID work at some point, but when I upgraded Elementor Pro recently, it stopped working. 

    Sounds like Elementor team may be able to provide more information if you ask a more specific question about one filter at a time and any differences between the version you upgraded from and to.

    functions.php:

    There appears to be a lot of shorthand code and optimizations which could break in many ways. Without access to the debug output or data structure, below are some comments to consider or check regarding things that could possibly go wrong:

    /** TOKEN page → Related LOCATIONS (field_id=86669) */

    function aa_query_locations_by_token($query){

    global $wpdb;

    // Resolve token ID even in AJAX preview

    The code begins with a comment that this query should be occurring on a token page, but then attempts to get the token ID several different ways, and attempts to get the related IDs by a field ID 86669…

    $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);

    }
    • Did this logic actually result in finding the IDs of the desired objects of post type oum-location? Try outputting $ids to verify.
    • If no $ids, did $token_id resolve correctly? If you’re not actually on a token singular template, it may be sufficient to use get_the_ID() to get the token ID, skipping the other logic.
    • The code below Defensive: can be removed, as you already set the post type and post_status on $query in the following lines.
    • It’s not necessary to guess at what values exist or not on WP_Query; you can override the object with a fresh instance.

    Without knowing your exact data structure or where this is being queried, I would replace the entire function with something like the code below, assuming the post type is token and the relationship field name is rel_locations:

    <?php

    function aa_query_locations_by_token( $query ) {
    $token_post_type = 'token';
    $location_field_name = 'rel_location';

    if ( $token_post_type === get_post_type( get_the_ID() ) ) {
    $token_id = get_the_ID();
    }else {
    $token_id = get_queried_object_id();
    }

    $location_ids = get_post_meta( $token_id, $location_field_name );
    if ( empty( $location_ids ) ) {
    $location_ids = [ 0 ];
    }

    aa_emit_footer_note(
    sprintf(
    'Token ID %d is related to location IDs %s through field %s.',
    $token_id,
    implode( ', ', $location_ids ),
    $location_field_name
    )
    );

    $query = new WP_Query(
    [
    'post_type' => 'oum-location',
    'post_status' => 'publish',
    'post__in' => $location_ids,
    'orderby' => 'post__in',
    'posts_per_page' => -1,
    'no_found_rows' => true,
    ]
    );
    }
    add_action('elementor/query/locations_by_token', 'aa_query_locations_by_token', PHP_INT_MAX, 1);

    …if that works, then move on to paging/offsets or additional filters from there. If it doesn’t work, look closer at where the token ID is coming from and whether the post types and IDs are as expected.

    Thread Starter vindic8r

    (@vindic8r)

    Thank you for the reply, @pdclark ! I appreciate it.

    At this link to my server, you’ll find my full functions.php, screenshots of my tokens and oum-location pod field structures. I’ve also included my debug.log, which is mysteriously missing most of the debug output I expected to see.

    I replaced my current aa_query_locations_by_token function with the one you provided and the page no longer loaded properly. I’m assuming that perhaps there’s nuance of my data structures that need to be considered? I’ve commented that version of the function out and replaced it with my original code, so that the page now renders. In the page source, I’m seeing this debug output, as expected:

    <!-- AA_DEBUG: FINAL {"hook":"locations_by_token","token":86681,"vars":{"posts_per_page":6,"paged":1,"post_status":"publish","post_type":"oum-location","orderby":"post__in","author":"","s":"","post__in":[86689,86690,86693,86694,86695,87429],"tax_query":[],"meta_query":[],"offset":0}} -->

    This output includes all 6 of the related locations.

    The shortcode test output on the page is powered by another function which uses the same query. Because the output is accurate, I believe I’ve proven that it’s not the query that is the issue, but something to do with the query hook not firing?

    Please let me know if there’s any further context I can share to help get to the bottom of this issue.

    • This reply was modified 8 months, 3 weeks ago by vindic8r.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Loop Carousel with Related Items’ is closed to new replies.