Skip Navigation

[Résolu] Manipulate and Display Views with PHP

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem

How can I alter the output of a View, and display that output with PHP?

Solution

You can directly get and display an entire View, Content Template or Query Result using the Views API functions.

If you need to manipulate any aspect of the View with Code before you display the results, you can use the Views Filters instead.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Marqué : ,

This topic contains 7 réponses, has 2 voix.

Last updated by Beda Il y a 3 années et 6 mois.

Assigned support staff: Beda.

Auteur
Publications
#621541

OK, i have a map with 100 markers and under it, a list of the marker shown on the map... that work fine.
But because some marker pay $$$ to get a better place in the list i dont just what a list sorted in alphabetical order, but here is what i what

marker that pay, will be in red on top of the list (in alphabetical order if more that 1)
marker that does not pay, after in grey in alphabetical order...

in fact i what a DOUBLE output sorted alphabetically...

how to do this, is it possible to do it... and if i have to write my own shortcode, it's fine, but get me the PHP function i will have to use to get the marker list in the map...

thanks in advance

#621674

Create a Custom Field that tells the program the post is by a "Paid" user.

This Field can be anything.
A simple single line that says "alpha-post" for example.
The important is, the value must always be the same for all those paid posts.

You then create a View and Query your Post Type.

After, you Order by this new custom field you created.
Then, on the secondary ordering, you choose for example the Post Date.

In the Views Loop later you apply HTML conditional to assign different classes to a DIV that is holding the Post Information.
This conditional will check if the Post has the custom field value "alpha-post" and call a custom CSS class, while all others will call another, default CSS class.

This is the DOC that will provide you with the information on how to use the technologies I elaborated above:
Conditional: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This is the closest you can get with Toolset, not using any Custom code.

#621728

Not completely sure i understand what you tell me, and if this apply to me.... here is why i ask you more detail on the second part of my question. IF i what to sort/output it myself, how do i manage to get the maker list array in php and (this part i know) sort it, css color it and return it to show it as a list.

So my question is, what i use to get the array of the resulting search/show marker info php ?

#621734

As I explain here, you can practically use Toolset GUI features only; you do not need to use PHP.
https://toolset.com/forums/topic/marker-list/#post-621674

Unless in specific cases, Toolset is built to be used so to avoid PHP.

I will explain below the steps to take related to my previous explanation, using another example.

1. Create a Custom Field in Toolset > Post Fields (it can be a single line, or a radio, or similar, call it "paid user")

2. In your posts, you will know which posts are made by authors who "paid" and not. For each post by such a "paid" user, you save this Custom Field (let's say with value "paid-user").

3. In a View query the posts you use so you can later display them as markers (in a list, or however you like).

4. Add an "Order by" by the above-created custom field (paid-user) you created, in the view.

5. Then, on the secondary ordering, you choose for example the Post Date.

In the Views Loop later you apply HTML conditional to assign different classes to a DIV that is holding the Post Information.

This means you will call specific CSS Classes conditionally and hence, see the posts with your unique style if the field we created above is "paid-user".
An example:

[wpv-conditional if="( $(wpcf-paid-user) eq 'paid-user' )"]<div class="paid">[/wpv-conditional]
[wpv-conditional if="( $(wpcf-paid-user) ne 'paid-user' )"]<div class="another-class">[/wpv-conditional]
content
</div>

With these classes then you can add CSS conditionally.

If you want to get the results of a view by PHP, you can refer to this Document, where we explain how to do that:
To get Types Fields data: https://toolset.com/documentation/customizing-sites-using-php/functions/
To get Views Data: https://toolset.com/documentation/programmer-reference/views-api/
To manipulate Views Data: https://toolset.com/documentation/programmer-reference/views-filters/

You can already output lists in a View, hence it should not be necessary to do that with PHP.
If you want to build your own output, sometimes it is better to directly access the WP_Query, because Toolset Views is just a wrapper for the native WP_Query.

If you plan to build your output with PHP but take the input from Views, it is likely to just add a step that is unnecessary, since your code would be faster if you directly access the WP_Query.

Our Views Plugin does basically provide a GUI for this WP_QUery, so you can build your own searches in minutes, instead of programming them.
And, you can concentrate on HTML, CSS and JS instead of coding PHP templates.

If you need to get data from Views with PHP and return it styled, it usually means you could already get it from the WP_Query, and you could avoid an extra step by simply accessing the WP_Query class:
https://codex.wordpress.org/Class_Reference/WP_Query

Let me know if I can help further.

#625446

OK i understand in you example that THIS work
but what i will need, it the WP object or array send to php shortcode
because i will have to check a lot more field than in the example...
it's have something to do with subscription and more things
so i will have to go to the php way and i can code it, it's fine
but i need to have the array or abject list to sort it and test it agains subscription level
and more..

thanks

#625595

Well, I think in this case you will be interested in the Views API:

This is how you can directly get and display the entire View, Content Template or Query Results:
https://toolset.com/documentation/programmer-reference/views-api/

If you need to manipulate any aspect of the View with Code, you can use the Filters instead:
https://toolset.com/documentation/programmer-reference/views-filters/

With this API you will be able to manipulate and receive the Views Loop and more.

#625780

I will do some test, and will close this tread if it fit my needs... thanks !

#625821

OK, please let me know if you have any doubt!