Skip Navigation

[Resolved] User data shows only custom post type in search

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

Problem:

The customer had a search view that was supposed to show both custom post types (CPTs) "listings" and standard posts, based on the fields "city" and "listing category." However, when users were redirected to the search from their account, only "listings" were shown, and standard posts were not included in the results, even though they should have been.

Solution:

The issue was due to a discrepancy between the "listing category," which was set as a taxonomy for the "listings" CPT, and a custom field ('wpv-listing_category') for standard posts. The provided code was only testing for taxonomies and not for the custom field, causing standard posts to be excluded from the results. We modified the code to check both the taxonomy and custom field values, ensuring that both "listings" and standard posts would be displayed based on the search criteria.

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.

Our next available supporter will start replying to tickets in about 2.35 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by Mateus Getulio 3 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2725554
8.PNG
7.PNG
6.PNG

Hi,
Thanks to the awesome support here, the website I've been working on has tons of features.

One of them is: user fills data, then is being redirected to search results, based on those criteria.
So far, the search described above, shows only custom post types(listings), but it should show custom post types and standard posts(they are also included in the search view).

The search parameters are "city" and "listing category", which are fields made for the custom post type.
Those parameters show both listings and post results, when I search trough the search template. However, results do not show posts when user is redirected to the search based on the parameters and it should.

I've tried editing the code with AI to make the meta not linked to the custom post type, but it did not work.
The code:
add_filter('wpv_filter_query', 'func_user_selected_city_listing_cat', 99, 3);
function func_user_selected_city_listing_cat($query_args, $setting,$view_id) {

if($view_id == 9980 and is_user_logged_in() and pms_is_member() ) {

global $current_user;
$user_id = $current_user->ID;

if((isset($_GET['cred_referrer_form_id']) and !isset($_GET['wpv_view_count']))) {

$user_city = get_user_meta($user_id,'city',true);
$user_listing_category = get_user_meta($user_id,'listing_category',true);

if($user_city) {
$query_args['meta_query'][] = array('key'=>'city',
'value'=>$user_city,
'type'=>'CHAR',
'compare'=> '=');

}
if($user_listing_category) {

$query_args['tax_query'][] = array(
'taxonomy'=> 'listing_category',
'field' => 'id',
'terms' => $user_listing_category,
'operator' => 'IN');
}
}
}

#2726201

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

If I understood correctly, you want to be able to search/filter by category and city which are fields that belong to the CPT listings, but always show posts regardless of the filters applied even though the default post type 'posts' doesn't have those fields assigned to it.

Is that correct?

Thank you,
Mateus

#2726718

Hi,
"listings" has "city" and "listing category" fields. Posts use those fields as well (user generated content trough upload form) and thus why when I search trough search view posts show, like they should.

I need posts to show based on "city" and "listing category" alongside cpt, when user is redirected from their account and views custom search based on user form.
Right now only cpt shows during the custom user search based on their form account data.

#2727428

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi,

Can you please share temporary admin login details along with a link to a page where this view can be seen?

We were working together in another issue but the credentials got removed from our system after the ticket has been marked as resolved due to security reasons, I apologize for the inconvenience.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Mateus

#2732462

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I checked it and it looks like for the CPT 'listings' the listing category is a taxonomy, but for posts, it is a custom field 'wpv-listing_category'. Hence you not finding any results for posts, since the code was testing the categories only for taxonomies.

I made a small change to the code so that it tests both taxonomy and custom field values for the category:

add_filter('wpv_filter_query', 'func_user_selected_city_listing_cat', 99, 3);
function func_user_selected_city_listing_cat($query_args, $setting, $view_id) {

    if ($view_id == 9980 && is_user_logged_in() && pms_is_member()) {

        global $current_user;
        $user_id = $current_user->ID;

        if (isset($_GET['cred_referrer_form_id']) && !isset($_GET['wpv_view_count'])) {

            $user_city = get_user_meta($user_id, 'city', true);
            $user_listing_category = get_user_meta($user_id, 'listing_category', true);

            // Initialize meta_query if it doesn't exist
            if (!isset($query_args['meta_query'])) {
                $query_args['meta_query'] = array();
            }
          
            $query_args['meta_query'] = array(
                  'relation'  => 'OR'
              );  

            if ($user_city) {
                $query_args['meta_query'][] = array(
                    'key' => 'city',
                    'value' => $user_city,
                    'type' => 'CHAR',
                    'compare' => '='
                );
            }

            if ($user_listing_category) {
                $query_args['meta_query'][] = array(
                    'key' => 'wpv-listing_category',
                    'value' => $user_listing_category,
                    'type' => 'CHAR',
                    'compare' => '='
                );
              
              $query_args['meta_query'][] = array(
                'taxonomy' => 'listing_category',
                'field' => 'id',
                'terms' => $user_listing_category,
                'operator' => 'IN'
              );
            }
        }
    }

    return $query_args;
}

I tested the search preference form at hidden link and it seems to be working coming from the form now.

Can you please check it?

#2732860

Thank you, that fixed it.