Skip Navigation

[Resolved] how to display a view of posts related to post by shared custom field value via

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

Problem:

The issue here is that the user wanted to filter their current view by the custom field on the current page.

Solution:

In this case the user was using the Classic Editor for Blocks to create their view so they are able to use this method below.

What you need to do is to add a query filter to your view and then set the query filter to get the value from a shortcode attribute.

Then you will be able to filter the view by passing the shortcode directly into the view like this.

[wpv-view name='my-view' param="[types field='my-field'][/types]"]

Where my-view is the slug of your view "param" is the shortcode parameter you've set in the filter and "my-field" is the slug of the field.

If you are using the Block view then this can be done by using the Hook below.

add_filter( 'wpv_filter_query', 'filter_by_post_customfield',99,3 );
   
function filter_by_post_customfield( $query_args,$view_settings,$view_id ) {
  
    if ( $view_id == 4187) {
      $post_id = get_the_ID();
      $meta_val = get_post_meta($post_id, 'wpcf-parent-model-name-custom-field' );
   
        $query_args['meta_query']= array(
          array(
                  'key'     => 'wpcf-parent-model-name-custom-field',
                  'value'   => $meta_val[0],
                  'compare' => '=',
              ),
          );
    }
    return $query_args;
}

To use the Hook add it to your Toolset Custom code section in Toolset -> Settings -> Custom Code and add it. Ensure that it has been activated.

Now what you need to do is to change the 4187 to the ID of your view and change "wpcf-parent-model-name-custom-field" to the slug of the custom field that you want to get the value for on the current page keeping the wpcf- prefix.

Next replace the second instance of wpcf-parent-model-name-custom-field to the field slug that you want to check for on the post type that the view is listing. In this user's case both fields were on the post being queried.

This support ticket is created 3 years, 8 months 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
- 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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by esmondR 3 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2035587

Tell us what you are trying to do?
i want to have a list (view) of posts related to the post being currently viewed, linked by a shared value within a custom field

Is there any documentation that you are following? only what i have found on your website

Is there a similar example that we can see? not sure

What is the link to your site? hidden link

#2035905

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Esmond,

Thank you for getting in touch.

You want to filter your view based on the custom field of the current page being viewed correct?

Are you building the view on the content template for the post type or are you creating your view using the classic editor at Toolset -> Views?

Please let me as we can filter the view by using the shortcode attribute, however I need to know how the view was created.

Thanks,
Shane

#2036503

Hi Shane,

I am using the classic view editor, and i have the view being displayed within an oxygen template for the custom post type, 'equipment-models'. The reason for the shared custom field is that some of the 'equipment models' are just variations of the same model and I want to be able to group them as 'siblings' or 'child' of a 'parent' model.

I have also created a Taxonomy called 'equipment-model-parent-taxon' as a means of grouping the 'siblings' or 'child' of a 'parent' model which does work, but wondered if this was also possible by shared custom field?

In the end there may be 1000's of equipment models and I am using spreadsheets to compile data and then upload using wpallimport with the toolset addon.

The site is password protected so i will share log in details with you if you want to have a look around. It is only a production site while I try and work this out.

#2036793

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Esmond,

Yes sharing the credentials will be really helpful.

However yes you are able to filter the view by the current post's custom field value. What you need to do is to add a query filter to your view and then set the query filter to get the value from a shortcode attribute.

Then you will be able to filter the view by passing the shortcode directly into the view like this.


[wpv-view name='my-view' param="[types field='my-field'][/types]"]

Where my-view is the slug of your view "param" is the shortcode parameter you've set in the filter and "my-field" is the slug of the field.

Please let me know if this helps. I've also enabled the private fields for your next response as well.

Thanks,
Shane

#2037319

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Esmond,

Yes that is correct the Oxygen builder doesn't play nicely with the nested shortcode and not much we can do about that.

However there is an alternative, you can use the view filter hook to filter it automatically . Add the following to your custom code section in Toolset ->Settings -> Custom code and activate.

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author',99,3 );
 
function prefix_show_only_current_author( $query_args,$view_settings,$view_id ) {

    if ( $view_id == 4183) {
      $post_id = get_the_ID();
      $meta_val = get_post_meta($post_id, 'wpcf-parent-model-name-custom-field' );
 
        $query_args['meta_query'] = array(
    'meta_key'     => 'wpcf-parent-model-name-custom-field',
    'meta_value'   => $meta_val,
    'meta_compare' => '='
);
           var_dump($query_args);
    }
    return $query_args;
}

You will need to remove the existing query filter first before using the above.
Please let me know if this helps to filter your view now.

Thanks,
Shane

#2037405

Hi Shane,

I tried as you suggested, but this is what was outputted on the front end - please note that i had created a new view and changed the view id in your code that you kindly provided.

array(12) { ["post_type"]=> array(1) { [0]=> string(15) "equipment-model" } ["paged"]=> string(1) "1" ["suppress_filters"]=> bool(false) ["ignore_sticky_posts"]=> bool(true) ["ep_integrate"]=> bool(false) ["posts_per_page"]=> int(-1) ["post__not_in"]=> array(1) { [0]=> int(3736) } ["wpv_original_limit"]=> int(-1) ["wpv_original_offset"]=> int(0) ["wpv_original_posts_per_page"]=> int(-1) ["post_status"]=> array(2) { [0]=> string(7) "publish" [1]=> string(7) "private" } ["meta_query"]=> array(3) { ["meta_key"]=> string(35) "wpcf-parent-model-name-custom-field" ["meta_value"]=> array(1) { [0]=> string(10) "RAV KPX336" } ["meta_compare"]=> string(1) "=" } }
RAVAGLIOLI 509 RAVAGLIOLI KPH370.40KEX RAVAGLIOLI KPX341 RAVAGLIOLI 4351.3IMOT4 RAVAGLIOLI 4402BMW RAVAGLIOLI KPH370.55 RAVAGLIOLI 4405ESI RAVAGLIOLI KP5.349WELIK RAVAGLIOLI 635.4 RAVAGLIOLI KPS326 RAVAGLIOLI 4401DC RAVAGLIOLI KPH370.32R RAVAGLIOLI KPS306C2 RAVAGLIOLI 4......(it then continues to list custom post types)

#2038405

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Esmond,

I did a few more checks on the code and managed to correct the issues.

The final code is now.

add_filter( 'wpv_filter_query', 'filter_by_post_customfield',99,3 );
  
function filter_by_post_customfield( $query_args,$view_settings,$view_id ) {
 
    if ( $view_id == 4187) {
      $post_id = get_the_ID();
      $meta_val = get_post_meta($post_id, 'wpcf-parent-model-name-custom-field' );
  
        $query_args['meta_query']= array(
          array(
                  'key'     => 'wpcf-parent-model-name-custom-field',
                  'value'   => $meta_val[0],
                  'compare' => '=',
              ),
          );
    }
    return $query_args;
}

Please let me know if this helps.
Thanks,
Shane

#2038439

Thanks for finding a solution, this works well. My issue is resolved now. Thank you!