Saltar navegación

[Resuelto] Sort a list of entries by the customers name with headings

This support ticket is created hace 5 años, 10 meses. 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.

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)

Etiquetado: ,

Este tema contiene 18 respuestas, tiene 2 mensajes.

Última actualización por Nigel hace 5 años, 9 meses.

Asistido por: Nigel.

Autor
Mensajes
#1196689

HI, My site I am working on is enlace oculto.

The setup A consultant (cpt) will load a customer (cpt) into system. There will then be "requirements" (cpt) linked to the "customer".
My challenge to create a view where only the "customer's" "requirements" for the logged in "consultant" will be shown in a view when the consultant runs a report. The report must follow the following layout:

<h4>customer1</h4>
1. Requirement
2.Requirement etc.
<h4>customer2</h4>
1. Requirement
2.Requirement etc.
<h4>customer3</h4>
1. Requirement
2.Requirement etc.

In short, the report must show only the logged in consultant's customers with their respective requirement listed under their name.

I have created all the cpt's already with the required relationships and created cred input forms that is working already.

#1196780

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Sorry, I didn't get chance to look at this in time before closing, I'll be on it in the morning.

#1197064

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Where are you displaying this list?

It looks like you need nested Views.

An outer View loops through customers. It will need a Query Filter so that it only shows customers related to the consultant. The specifics will depend upon where you are inserting the View.

You will then need a second View to display the requirements related to the current customer from the parent View, again using a relationships Query Filter. You insert this second View in the loop section of the first View, after the heading.

If you have problems determining what should be in the Query Filters, I'll need to know more about where the parent View is inserted.

#1197179

Hi Nigel,

Ok first of the Consultant is not cpt. It is just a WordPress native user with a specific role of a consultant. Would it be required or recommended to create a cpt for the consultant to achieve this goal?

#1197279

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

You stated in your opening question that the consultant was a CPT 😉

How are customer posts related to the consultant users? Is the consultant the author of the customer post, is that the connection?

If that is the case then your parent View needs a Query Filter for the post author (not a relationship filter). Again, the details of the filter depend upon where the View is being shown. It might be the current logged-in user, for example, so that when a consultant visits a page where the parent View is inserted, they will see "their" customers.

#1199114

Hi Nigel,

Sorry for delay in replying, Here is the full picture

We the admins will open an "account" for a consultant. The consultant must have login rights.

The consultant will then add his customers to a cpt, and add a user(user) for each customer (cpt).
a consultant will have many customers on the system.

The consultant will then add a list of required documents (ctp with a relationship) for each customer. The important point here is that the consultant must only be able to access their clients they loaded on the system Ie being the author.

The customer user must then be able to view a list of the documents required for action. This Customer user will be a co-author of the customer post. (remember the long discussions regarding multiple authors for a post).

So the important point here is a consultant must only be able to view his Posts of clients and obviously related entries. A customer user must also be able to view requirements created for his company and action them.

A synopsis of what happens here is as follow:

In our country, every business must comply with a regulation called bbeee. To achieve this the company must annually hand in a set of legal documents. In the region of 45 documents. This has created a business opportunity for consultants to assist the companies to comply with these requirements. Our business model is to assist these consultants with the tracking of the required documents.

The full flow of work should be:

We admin create an account for the consultant ( question here is now should this be just a user or a cpt and user). Each consultant then manages his portfolio of customers (cpt). He adds new customers to the system and then adds one or more users for each customer to action on the company and link them to the company.

The consultant then selects a group of documents (cpt) that are required (ctp) to achieve the goal. The user/s for each company must then open a list for their company and click on the required document to open an upload screen where they will then upload the document for verification by the consultant. The Consultant with then either approve or query a document. If querying the file will then reappear on the required list with the comments from the consultant for action. The document can then be reuploaded by the company user once the changes have been affected. Once the document is correct the entry will be marked as completed.

The whole idea behind this is to track outstanding documents for each consultant' portfolio of customers and keep a repository of the documents on the server. This process is done annually,

Now, this is where the question originated. A consultant must only be able to access his portfolio of companies he is the author of. A company user must only access His company he is a user of and the document list of all the required documents for his company.

Now back to my question do i need a cpt for the consultant or will e user only work in this instance.

#1199192

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

It seems like you want to hang this on being able to assign multiple authors to posts using the 3rd-party plugin you needed help with earlier. Before going much further I should re-iterate that adding multiple authors is not standard WordPress functionality, and although I'm superficially aware of how the plugin works, when it comes to the details you will need to identify what is required to get it to work with Toolset.

To summarise my understanding of your set-up, a consultant (user) creates customers, meaning they register a WordPress user on behalf of the customer, and also create a customer CPT, and make both themselves and the newly registered user authors of that customer post. (Requirements are a CPT related to customers.)

Normally, to display a View of customers that belonged to the currently logged-in consultant you would create a View to display customer posts with a Query Filter to show only posts where the post author was the currently logged-in user.

This is the first point of failure for your multi-author set-up.

My recollection is that the multi-author plugin uses taxonomies to set the post authors.

So you cannot depend on the post author Query Filter, you would need to add a taxonomy filter instead (for this taxonomy that the multi-author plugin creates), but the "currently logged-in user" wouldn't be an option available in the UI to set the term.

You would need to get the value for the filter from a shortcode attribute which you supply where you insert the wpv-view shortcode, and you should be able to pass the ID of the current user using the wpv-user shortcode. (I don't know if this will work; I don't know whether the user ID is what is required for a taxonomy filter for how the multiple author plugin stores the author data.)

There is a potentially more problematic point of failure.

Do the consultants have back-end access? You can use Access to specify that users with a particular role can only edit their own posts, for example, rather than others', but if your "post authors" are not really post authors because they are a taxonomy, this cannot work.

It seems like a better solution to not use the multiple author plugin, and to use post relationships to manage the connections.

Because you can only connect posts and not users, you would need to make a CPT for consultants (which the consultant users were authors of) as well as a CPT for customers (which the customers were authors of), and connect consultant posts to customer posts in a relationship.

You would then use nested Views as required, so, for example on a page where the currently logged-in consultant should see a list of their customers, you would have an outer View to return the consultant post of the current user, and in the output section of that View you would insert another View to show the customer posts that were related to the current post from the parent View.

You might still have a problem limiting access in the back-end, but I'm not sure from your description if that is required or not, and in that case you might need to make the consultant the author of the customer posts, too, and use a custom field on the customer posts to identify the user they belong to.

Let me know your thoughts on this before we proceed.

#1201555

HI NIgel,

Thx for your reply. I fully understand your reservations with the multi-user setup. I do think I understand where you are going with your approach to do this with relationships. I may have to call on your assistance where i get stuck in this approach.

To recap your approach:
You create a user for a consultant. Then I create a cpt for the same consultant and make the user the author of the cpt entry. Then created a user for the client and make the user the author of the client cpt entry. Then I create a relationship between the consultant and the client.

The back end access is not required for either user. So we can totally control it with access. I will try this approach and revert back to you if and when i need extra assistance

#1201623

HI Nigel,

It seems your approach may just be the way to go. My first question. Is it possible to change the author of a post with cred forms? Ie when admin loaded the consultant user and are now in the consultant form adding a consultant page, that there would be a drop down for users that the admin can make the consultant's user the author of the consultant cpt. I can not find information on setting a user as the author of the consultants cpt.

#1202284

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Forms cannot set the author of the post (if this is a "publish" form, then the current user will be the author, if this is an "edit" form, then the post author is unchanged).

You can do it, of course, but you would need to write a PHP snippet using the cred_save_data hook or similar to modify the post author when the form is submitted.

You could use a generic select field to specify which use should be made post author.

(And you could use a View to populate that select dropdown; it would need to be a "clean" View with the wrapper disabled, and output options in the same JSON format as if you entered some options manually for the generic field.)

#1202936

Thx for this info, Next question how do I set my code that I can select the author of the post and not change it to the current author.
The reason for me aking this is that the Admin will have to add the consultant user and consultant cpt and make this user the author of the cpt. As far as i have been able to establish is that cred_save_data hook will only update the author automatically.
My problem is not much the admin being able to do it it will be when the consultant adds a company user and company cpt and then this user must become the author of this cpt. This consultant will not have back end access to the system so i must figure out away to achive this with a cred form somehow.

#1203797

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

The first step is to add a select field to your form that lists the users who could be the post author.

The option value should be the user id, the label the user display name.

So create a View to display the relevant users (by role?).

You need to output a JSON list of values and labels, so your output section will need to look like this:

		<wpv-loop>
			[wpv-item index=other]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='display_name']"},
			[wpv-item index=last]
				{"value": "[wpv-user field='ID']", "label": "[wpv-user field='display_name']"}
		</wpv-loop>

Also, disable the wrapper div (but beware of this issue: https://toolset.com/errata/after-updating-to-views-2-7-3-the-checkbox-for-disable-the-wrapping-div-around-the-view-does-not-work/).

Then create or edit your form for publishing posts where you want the post author to be selectable.

Insert a generic select field and provide the values from the View you just created. (You'll need to do this in expert mode as this doesn't work in visual mode; I noticed that while preparing a test for this and have reported it, but there is no erratum yet.)

That will look something like this:

[cred_generic_field type='select' field='selected-author']
{
"required":0,
"options":[ [wpv-view name='user-json-list'] ]
}
[/cred_generic_field]

Now your front-end form should have a user selector to choose the post author.

The next step is to actually assign that post author using the cred_save_data hook. When that hook is triggered the post has already been saved (with the current user as post author), and we will be taking the value from the generic field to update the post with a different author.

You can use code such as the following to do that:

add_action( 'cred_save_data', 'tssupp_form_submit', 10, 3 );
function tssupp_form_submit( $post_id, $form_data ){

	if ( in_array( $form_data['id'], array( 77 ) ) ) { // Edit form ID

		if ( isset( $_POST['selected-author'] ) ){

			wp_update_post( array(
				'ID'			=>	$post_id,
				'post_author'	=>	$_POST['selected-author']
			) );

		}

	}
}
#1206096
track doc custon code snippit.png
trackdoc add consultant form back end.png
trackdoc add consultant frontned form.png

HI Nigel,

I have proceeded as set out in the ticket above but the "select field" is still not showing in the form. I am attaching all relevant screen prints. I have noticed in the select field code you have added two [[ ]] around the json view name. I have tried to remove a pair but it still made no difference.

#1206132

Nigel
Supporter

Idiomas: Inglés (English ) Español (Español )

Zona horaria: Europe/London (GMT+00:00)

Did you apply the plugin updates published yesterday evening?

There were issues with Views (disabling the wrapper div) and Forms (saving generic select fields where the options were provided by a shortcode) that might have prevented you from getting this working.

One check you can make is to directly output the View used to generate the JSON options on a test page and make sure they look formatted correctly (the same as if you manually added some options to a generic select field).

If they are being output correctly the select field should work in the form.

If when you view the form on the front-end you don't see the select field then that is likely where the issue lies (and everything else, the custom code using cred_save_data, is not yet relevant).

#1206876

HI Nigel,

The json part is working. It is showing the entry on that view. However, on the cred form, the change author field is still hiding.

Here is the code used for the generic select field

<div class="form-group ">
      <label>Change Author</label>
        [cred_generic_field type='select' field='selected-author']
        {
        "required":0,
        "options":[wpv-view name='user-by-consultant-level'] 
        }
        [/cred_generic_field]
      </div>

The label show but the field itself is still not visible