Skip Navigation

[Resolved] Saving a Users Nickname To a Post Field And Then Using it for Archive Search

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to add the name of each filter option to the text shown in each option of a search View's filters. Currently only the count of results in each option are shown.

Solution: Use the format attribute of the wpv-control-postmeta shortcode to define the proper format. In this case, it was necessary to create a new Layout for some reason as the existing Layout had become corrupted.

[wpv-control-postmeta field="wpcf-dog-rescue" type="select" default_label="All rescues" format="%%NAME%%
 (%%COUNT%%)" url_param="wpv-wpcf-dog-rescue"]

Relevant Documentation: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-control-postmeta

This support ticket is created 7 years ago. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#587274

I'm trying to get cred to save the current users nickname to a custom field when they create a post via a front end form. This field is then to be used as part of a parametric archive search using a select box - in effect a work around for not easily being able to mix user and post fields in a view search / filter

Noman has kindly helped add a filter acting on the userfield directly (https://toolset.com/forums/topic/autocomplete-a-custom-post-field-with-the-users-nickname-when-using-cred/) but its not as "complete" as searching by post fields and for the intended use using a post field containing the nickname would be fine and a more compatible with the other search fields.

I can get cred to save the logged in users nickname to a single text field with this code

 [cred_field field='dog-rescue' post='dog' value='[wpv-user field="nickname"]' urlparam='' class='form-control' output='bootstrap']

By using this filter

<div class="form-group">
	<label>[wpml-string context="wpv-views"]dog-rescue[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-dog-rescue" type="select" default_label="All rescues" format="(%%COUNT%%)" url_param="wpv-wpcf-dog-rescue"]
</div>

the number of matches is shown and updates correctly when the other filters are changed but it doesn't display the actual content of the fields.

How can i populate this select box with the contents of the custom field?

#587319

Hi, are you trying to add the "name" of each option to the count displayed in each option? If so, you can modify the format attribute of the wpv-control-postmeta shortcode:

[wpv-control-postmeta field="wpcf-dog-rescue" type="select" default_label="All rescues" format="%%NAME%%
 (%%COUNT%%)" url_param="wpv-wpcf-dog-rescue"]

This will give you options with the nickname followed by count:
- (20)
- john.c (15)
- george.a (5)

The first option represents the "all" value. More information about the wpv-control-postmeta shortcode format options here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-control-postmeta

#587350

Hi Christian,

Thanks for the quick reply 🙂 My bad on the %%NAME%% option, it was there at one point but must have missed it when putting the code back in again after trying something else...

I've added the missing option but no joy - apart from the default label and (%%COUNT%%) the select box is still blank.

The form is saving the field correctly as I can see it populated when editing the post on the backend and it also displays when adding the field to the loop output but for whatever reason the select box is empty.

#587567

Okay, that's strange. The same code works for me, so I suspect something else is going on. Can you try the following troubleshooting steps?
- Make sure your Toolset plugins are up-to-date
- Activate a default theme like Twenty Seventeen and deactivate all plugins except Types and Views, retest
- Open the browser inspector and refresh the page with the View and search controls. Are any JavaScript errors displayed?
- Enable server logs. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Refresh the page with the View and search controls. If any back-end errors are generated, this will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php.

#587691

Hi,

Done the troubleshooting and....

Make sure your Toolset plugins are up-to-date
Checked and everything was already upto date

Activate a default theme like Twenty Seventeen and deactivate all plugins except Types and Views, retest
Tried it with Twenty Seventeen and still not working - i've used layouts to customise the archive so had to leave that enabled too otherwise it just loaded the default archive without the search section.

Open the browser inspector and refresh the page with the View and search controls. Are any JavaScript errors displayed?
No errors are showing in either Firefox or Internet Explorer, i've also checked with a couple of mobile browsers it doesn't work on them either.

Enable server logs.
Done this and no errors are logged.

Sorry it wasnt better news.... I did wonder at first it it wasn't possible to display single line text fields as a select box but searched around and saw that others had done it without issue.

Thanks again

#587704

Okay in that case, I'll need to take a look under the hood and see why things aren't working as expected. It looks like you're working on a local development environment. Is it possible to put the site online somewhere I can access it? If not, can you use the Duplicator plugin to create a clone of your site? You can post a download link using Dropbox or Drive or some other file sharing service in the private reply fields here.

#587817

This is unusual, I think that something has become corrupted in this Layout and the WordPress Archive created specifically for this Layout. I was able to get the filter to work using this process:
- Delete the WordPress Archive cell from the Layout applied to Dogs Archives, resave the Layout
- Go to Toolset > WordPress Archives and create a new Archive for Dogs that includes the custom field filter on dog-rescue, uses the same AJAX settings, and the same Loop Output code as the previous archive cell.
- Return to the Layout for Dog Archives and insert a new WordPress Archive cell. Select the archive you just created, insert the cell and save the Layout.
- Go to /dog to see the results.

Let me know the results.

#587826
wp_archive_difference.jpg

That's worked at this end ????

Not sure if it helps or not but I originally started a new layout, filled the row by dragging the columns, selected archive cell and then created the new archive - this was done for both the "first" and "test" archives, neither of which worked.

Your method creates the archive view and then adds it to a layout.

I've attached a screen grab as the WordPress Archive page shows a difference between my attempt which displayed the same message as the "WordPress Archive" and your way which shows something similar to a shortcode.

If its ok with you I'll leave this ticket open until I add my intended loop output / filters and see if it still works (?) - suspect it will but want to double check.

Many thanks ????

#587828

Strange - I'll keep an eye out for similar issues and if it looks like this needs more investigation I can take a closer look. I wasn't able to replicate that locally, though, so it must be a specific series of events that caused that problem. At any rate, I'll stand by for your update on the filters and so forth.

#587852

Thanks Christian - it's working as expected.

Have also noticed the edit archive screen for this new view has some presets for the loop selection that include "Home/Blog", "Search results" and "Author archives" along with the other standard WordPress archive types - my original efforts and the auto created "WordPress Archive" didn't / don't show this.

Either way the search now include a filter for the authors nickname so all is well 🙂