I added a dropdown for my vendors to change the status of their vendor profile to draft
/publish
so they can take their profile offline whenever they want. The problem is that when it is a draft, all of the custom fields on their settings page disappear. Is there a way to prevent this from happening? I’d like them to be able to edit their profile without it being public. Thanks in advance.
Hi,
Unfortunately, there is no such possibility because the code checks whether the vendor is already confirmed or not, and if it is not published, it means that it is a regular user and there are no vendor fields. We recommend that you better make some custom meta field, for example, a checkbox (you can use vendor attributes for this), and then filter the results through the pre_get_posts hook
, and then check that the query vendor is on the frontend, etc. (for example, if the checkbox is checked, then hide).
Thank you. That was my back-up plan.
Got it working for the most part. I ended up modifying my drop down instead of a checkbox, then adding JS that changes a display_status
meta key on the vendor profile as well as the vendor’s listings via Ajax. This way the profile and listings can be hidden and the user doesn’t have to hit save and reload the page to begin editing. My only issue is trying to target the vendor and listing blocks on the homepage as well. Do you have way of checking if the query is in those blocks? Alternatively I can leave it open on all pages except for the settings as it causes issues on the settings page if I leave it. I can check for the current url to be the settings page since get_the_ID()
doesn’t work, unless you have a better suggestion.
add_action( 'pre_get_posts', [ $this, 'hide_drafted_profiles' ] );
public function hide_drafted_profiles( $query ) {
$post_type = $query->get( 'post_type' );
if ( !is_admin() && ( is_post_type_archive( 'hp_vendor' ) || is_post_type_archive( 'hp_listing' ) ) && ( $post_type == $this->post_type || $post_type == 'hp_listing' ) ) {
$query->query_vars[ 'meta_query' ][] = [
'relation' => 'OR',
[
'key' => 'display_status',
'value' => 'draft',
'compare' => '!=',
],
[
'key' => 'display_status',
'compare' => 'NOT EXISTS'
],
];
}
} // End hide_drafted_profiles()
I also made a shortcode for the vendor and listing pages to check if they are drafts, and they are and the current user is not the vendor, then it redirects them to an “under construction” page. That way anyone going directly to the pages won’t see them either, but the vendor can still preview them while editing.
I’m sorry for the delay.
I recommend changing the condition so it will not strictly check the post type archive, you can add the “or” condition to also check all the listing and vendor queries, possibly in conjunction with the “is_home()” check to affect those queries on the home page (with the Listings and Vendors blocks).
Hope this helps.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.