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.
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...
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 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."
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 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.