Friday, September 23, 2022
HomeWordPress Developmentphp - WP question with variables provides no end result for particular...

php – WP question with variables provides no end result for particular person


I am attempting to fetch one customized put up with two values: the logged in person ID and a identified string, each as variables.

The question works out high quality when testing on an admin account the place the person ID is 1 and exhibits the proper customized put up with my question. Nevertheless, after I take a look at the identical question logged in on an everyday person the place person ID is 4, the question provides no outcomes.

Once I insert the hardcoded knowledge instead of both of the variables within the question, it does present the proper end result, regardless of being the very same values and kinds when var_dump()ing them and no matter account.

My code for the question goes as follows:

$inner_args = array(
    'post_type' => 'any',
    'post_status' => 'any',
    'posts_per_page' => -1,
    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'track_url',
            'worth'     => $track_url,
            'examine'   => '=',
        ),
        array(
            'key'       => 'review_user_id',
            'worth'     => $current_user_id,
            'examine'   => '=',
        ),
    ),
);
$my_query = null; /// for security
//wp_reset_postdata(); /// modified nothing
//wp_reset_query(); /// additionally modified nothing
$my_query = new WP_Query($inner_args);

After the question I strive show knowledge from it with an ordinary whereas loop:

if( $my_query->have_posts() ) {
    whereas ($my_query->have_posts()) {
        $my_query->the_post();

        /*
         * Do issues with the queried put up
         */

    }
}

When checking the SQL request with echo $my_query->request;, the place one makes use of variables and the opposite hardcoded knowledge, each outputs are an identical. I’ve examined this on the admin account and common person account. Once more, former works with variables, the latter does not.

I figured one thing may be off when querying by WP_Query, so I tried the identical with a handwritten question (successfully copy-pasting the results of echo $my_query->request; and including my two variables). Its result’s an identical to the WP_Query end result, which isn’t any outcomes for the common person.

$my_query = $wpdb->get_results($wpdb->put together ( "
                  SELECT lju_posts.* FROM lju_posts INNER JOIN lju_postmeta 
                  ON ( lju_posts.ID = lju_postmeta.post_id ) INNER JOIN lju_postmeta 
                  AS mt1 ON ( lju_posts.ID = mt1.post_id ) WHERE 1=1 
                  AND ( ( lju_postmeta.meta_key = 'track_url' AND lju_postmeta.meta_value = %s ) 
                  AND ( mt1.meta_key = 'review_user_id' AND mt1.meta_value = %s ) ) 
                  AND lju_posts.post_type IN ('put up', 'web page', 'attachment', 'e-landing-page', 
                  'product', observe', 'assessment') AND ((lju_posts.post_status <> 'trash' AND 
                  lju_posts.post_status <> 'auto-draft')) GROUP BY lju_posts.ID 
                  ORDER BY lju_posts.post_date DESC", 
                  $track_url, $current_user_id ));

I’m querying on ACF fields, however I am not satisfied it is an ACF downside when it really works completely high quality when examined with an admin account and with variables, however does not work when examined with an everyday person account.

I’ve tried:

  • Altering person roles for person account
  • Making a contemporary person account and working by the method once more
  • Including 'NUMERIC' to area kind in meta arguments
  • Manually creating the customized put up by the wp-admin interface
  • Utilizing a hardcoded string for $current_user_id

I am unable to wrap my head round why the question fails to point out the customized posts once they exist, and with the proper fields as nicely. Why do variables seemingly break the question behaviour for one person however not for one more? If anybody has some perception into this phenomenon, I would tremendously respect it and hopefully be capable to transfer on.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments