I have a post-type created from Toolset Post Types "program-section" and it been set to "Managed by Access" in the "Access Control" setting with the settings as shown here. (hidden link)
When I login as a subscriber/customer and run the following query.
$args = array(
'post_type' => array( 'program-section' ),
'nopaging' => true,
'post_per_page' => -1,
);
// The Query
$query = new WP_Query($args);
d($query->found_posts);
This ideally should return with count of all the posts there is in the post type, but it returns 0.
If I change the setting to "Use the default WordPress read permissions" the code starts to return correct value.
How should I set the access control so that my WP_Query returns the correct posts.
Hi, I see a potential problem in this code:
$args = array(
'post_type' => array( 'program-section' ),
'nopaging' => true,
'post_per_page' => -1,
);
nopaging => true and post_per_page => -1 seem a bit redundant, so you could remove one or the other. However, the correct syntax for the latter is posts_per_page, with an 's', as per the WordPress documentation: https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
However, you mentioned that the correct number of posts is returned when Access management is disabled, so I'm not sure if this is part of the problem or not. Please try updating the code first, then if the problem is not resolved I can take a closer look. I would need to see a more complete code example, including the definition for function 'd' and the context of how the containing code is implemented, to know more about the context of the problem.
Hey,
Christian Cox, thanks for your reply.
I have removed 'nopaging' parameter and fixed 'posts_per_page' as well which fetches the same result.
The complete code is part of a function where I am trying to retrieve the program-sections according to the category which is passed to the funciton.
public static function getLessons(int $id, $category)
{
$programs = a(array());
$lesson_cat = $category->toArray()[0]->slug;
$args = array(
'post_type' => array( 'program-section' ),
'posts_per_page' => -1,
'meta_key' => 'wpcf-position-number-for-ordering',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array (
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $lesson_cat,
)
),
);
// The Query
$query = new WP_Query($args);
var_dump($query->found_posts);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$buffer = a(array());
$program_id = get_the_ID();
$program_title = get_the_title();
$program_order = get_post_meta(get_the_ID(), 'wpcf-position-number-for-ordering', false);
$getLesson = self::getProgramsLessons($program_id, $id);
$buffer->appendArrayValues([$program_id], 'program_id')
->appendArrayValues([$program_title], 'program_title')
->appendArrayValues([$program_order][0], 'program_order')
->appendArrayValues([$getLesson], 'program_lessons');
$programs->append($buffer->toArray());
}
}
// Restore original Post Data
wp_reset_postdata();
$sorted = $programs->customSortValues(function ($a, $b) {
if ($a['program_order'] == $b['program_order']) {
return 0;
}
return ($a['program_order'] > $b['program_order']) ? 1 : -1;
});
return $sorted->toArray();
}
the function [php]d()[php] is a PHP debugging tool(hidden link) which I use for the development process. It comes with this plugin for WordPress. (hidden link) and just dumps the value to the screen.
I have replaced it with var_dump which is used as default.
My query isn't returning the post with the above shared access setting for cusotmer/subscriber roles.
I am also using Arrayy(hidden link) package for Array manipulations which is used in the code.
1. toArray()
2. CustomSortValues()
Okay thanks for the additional information. Just to be clear, if you log in as an administrator and run the same query, does the query return the correct number of results?
Do these posts have an Access Post Group applied in the post editor?
If so, what are the Access settings for the Post Group(s) applied to these posts?
If not, I need to take a closer look. Please provide login credentials in the private reply fields here so I can see how this is configured.
Just to be clear, when I log in to staging at hidden link using the subscriber role credentials you provided, I see int (60) in the dump, not int (0). Should I test with a different User to see int(0)? Screenshot attached.
Also, can you explain where I can find the code you shared for this query? Is it in a custom code snippet, plugin file, or in the theme files somewhere?
Okay I can see the problem now. It seems that the Customer role cannot see Program Section posts unless the read access to the native WP Posts post type is also enabled to the Customer role in Access settings. If I activate Posts visibility for Customers in Access settings, the Customer role sees int(60). Do you plan to allow Customers to have read access to Posts, or do you plan to restrict Customers' access to posts?
We currently don't plan to use native WP Posts post type and the access of it doesn't really matters to us.
But why is it so that it is required to give the access of Native Posts type to enable other post Custom post type.
Asking for other website in which I may need this type of access setting.
It seems to have something to do with the way WooCommerce has defined permissions for the Customer role, because custom roles created by Access do not have the same problem. I'll ask my developers for more information, and ask if there is a better workaround. I'll let you know what I find out.
I'm still working on this. I haven't been able to replicate the same problem on a fresh site with only Types, Blocks, Access, and WooCommerce active. I'm downloading the Duplicator package that already exists on your staging site to try to isolate the problem and get more help from my 2nd tier support team.
I'm having some trouble. I was attempting to update Toolset plugins and WooCommerce on the site, but an error was triggered during the WooCommerce update process. I attempted to deactivate WooCommerce temporarily by renaming the plugin directory, but permission was denied to my SFTP user. Do you have the ability to delete or rename the woocommerce plugin directory in staging? Once that is complete, I should be able to reinstall the plugin and continue testing.