Skip Navigation

[Resolved] Return records published by current user

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 0.91 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 12 replies, has 2 voices.

Last updated by terryE 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#599111

I am trying to:

I have records (time-and-name), published by $user_id, attached to a product ($productID).

I want to query the records, attached to linked to product XXX, that was published by the current user.

//get current user
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;

//query DB > dates & times, with author ID and race ID
$the_query = new WP_Query( array(
'post_type' => array('time-and-name'),
'post_author' => $user_id,
'post_status' => 'publish',
'meta_query' => array(
array( 'key' => '_wpcf_belongs_product_id',
'value' => $productID,
'compare' => '==' )
)
)
);

//If any Posts are returned, we have some entries already
if ($the_query->have_posts()){
while ( $the_query->have_posts() ) {
$the_query->the_post();
//echo '

  • ' . get_the_title() . '
  • ';
    }
    }

    I expected to see:

    No records as current user has not submitted a record.

    Instead, I got:

    The code is returning records, that have been submitted by another user. It is ignoring 'post_author' => $user_id

    I have checked all the DB records and everything is correct, user_id is correct in the post record and the post_meta records are correct too.

    #599258

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi Terry

    I suspect your $user_id is not correct.

    Use get_current_user_id() for this:

    $user_id = get_current_user_id();
    
    #599345

    Hi Nigel,

    Thanks for this.

    Have echoed $user_id and it's giving me the current user id.
    Have change the code to your suggestion and it's giving me the same current user id.

    With either version, the 'post_author' => $user_id, seems to be ignored. 🙁

    Cheers
    Terry.

    #599356

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    What if you echo $the_query to your logs?

    That gives you an array of all the posts, including ones with the wrong post author?

    Can you try making a View with the same settings and see what happens?

    Make a View with a query filter for post author is the current author, and the post relationship filter.

    Do you see the same results as with your code?

    #599495

    With

    echo $query->request;

    The query that is actaully ran is....

    SELECT SQL_CALC_FOUND_ROWS wp_9posts.ID

    FROM wp_9posts

    INNER JOIN wp_9postmeta ON ( wp_9posts.ID = wp_9postmeta.post_id )

    WHERE 1=1 AND ( ( wp_9postmeta.meta_key = '_wpcf_belongs_product_id' AND wp_9postmeta.meta_value = '503' ) ) AND wp_9posts.post_type = 'time-and-name' AND ((wp_9posts.post_status = 'publish'))

    GROUP BY wp_9posts.ID ORDER BY wp_9posts.post_date DESC LIMIT 0, 10

    So looks like Author is being ignored for some reason...

    #599762

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi Terry

    I can't see what the problem is from here without examining your site for myself and seeing where and how you are using this code.

    Debugging custom code is outside our support policy, but if you want to provide me with a copy of your site I will try and take a look later to see if I can spot the problem.

    I'll mark your next reply as private so that you can share a link to the backup of your site: https://toolset.com/faq/provide-supporters-copy-site/

    When creating the backup please omit your uploads directory to reduce the size of the archives, I don't need your media files for this.

    #600160

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi Terry

    Can I get access to your site, the credentials you provided don't work.

    It will be quicker for me to go and take a duplicate than to use the files you provided.

    I'll mark your next reply as private.

    #600419

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi Terry

    I have a local copy of your site installed.

    I added a few error log messages in your code in the functions.php file of your child theme, which are not displaying.

    Your function doesn't appear to be running at all.

    If I do a text search for CheckIfOrdered I can only find the function definition itself. The function isn't used anywhere that I can see.

    Where do you call the function?

    #600493

    Hi Nigel,

    function is used in the Content Template > 'Template for Products'
    [wpv-conditional if="( CheckIfOrdered([wpv-post-id]) )"] {....}

    It will only run when viewing a product single page.

    Hope this helps,
    Terry.

    #600850

    Nigel
    Supporter

    Languages: English (English ) Spanish (Español )

    Timezone: Europe/London (GMT+00:00)

    Hi Terry

    I was still pretty sure the code was not being run because the messages I was trying to log from within the function were not appearing in my debug.log file, and so I installed the Query Monitor plugin to see exactly what queries were being run on the page, and it confirmed that your custom query was simply not being run.

    Looking at your content template where you have used the function inside a conditional statement, the reason is that your statement is incomplete, there is no value tested against.

    Note this line from the documentation "If you expect the function to return a boolean, the value to check agains should be 1 or ‘1’ for true and 0 or ‘0’ for false."

    https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/

    Adding a value to test against does trigger your function and the query does include a test for post_author = 8 with the user you provided details for.

    (I didn't do any other testing of your code.)

    #601229

    Hi Nigel,

    Thanks again for looking into this.

    If you check this URL...

    /product/elvis/

    It will show that the query is being ran and as there are no time entries, from any user, the form is being displayed.

    I had previously tried the 1/true, 0/false switch, but that made no difference 🙁

    I'll go through your post again and see what's what 🙂

    #601231

    Hi Nigel,

    I have done some more testing on the original product.

    With my function, I can see 5 queries being excuted that contain '_wpcf_belongs_product_id'

    With my function commented out, I can see 4 queries being excuted that contain '_wpcf_belongs_product_id'

    So my query is being executed.

    Query 71 is the one being exected by my function, and it does not contain author.

    Query 71 is being executed by Plugin: woocommerce-views, so I guess there is an issue with Toolset WooCommerce Views not parsing the author field when requested.

    I'll see if I can work around this by adding the author into the post meta and adding an 'AND' to my meta query.

    Thanks

    #601234

    Hi Nigel,

    I got around the "Toolset WooCommerce Views not parsing the author field when requested" issue, by saving the user id as part of the post meta data.

    >>> update_post_meta($post_id, '_wpcf_user_id', get_current_user_id());

    Then added it into my query

    >>> 'meta_query' => array(
    'relation' => 'AND',
    array( 'key' => '_wpcf_belongs_product_id',
    'value' => $productID,
    'compare' => '==' ),

    array( 'key' => '_wpcf_user_id',
    'value' => $user_id,
    'compare' => '==' )

    )
    )

    Everything now works as it should.

    Cheers
    Terry.