[Closed] Problems to show parent fields into posts with a view

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 8.34 hours from now. Thank you for your understanding.

This topic contains 16 replies, has 2 voices.

Last updated by Adriano 5 years, 10 months ago.

Assigned support staff: Adriano.

Author
Posts
#211476

Hello,

I want to show some parent fields into desired posts with a view. I read https://toolset.com/documentation/user-guides/displaying-fields-of-parent-pages/ but in all posts I insert my view I always get the same product.

I have the next structure.

Three Custom Types (Empresas, Smartphones and Tablets)

Empresas -> Smartphones -> Posts
-> Tablets - > Posts

Smartphones and Tablets are childs of Empresas

My view is called "Productos Relacionados" I attached you an image with the settings. And the layout code is

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
  <div id="related-product">
    <h3>Producto Relacionado</h3>
		<wpv-loop>
          <div class="producto-relacionado">
            [types field="abn_foto" size="thumbnail" id="$Smartphones" id="$Tablets"][/types]
            <div class="custom-fields">
              [wpv-post-link id="$Smartphones" id="$Tablets"]
              <p><strong>SO</strong> [types field="abn_android_so" id="$Smartphones" id="$Tablets"][/types]</p>
              <p><strong>Precio</strong> [types field="abn_android_precio" id="$Smartphones" id="$Tablets"][/types]</p>
            </div>
          </div>
		</wpv-loop>
  </div>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found][/wpv-no-items-found]
[wpv-layout-end]

Well, I always get the same result in all posts, for instance, in hidden link the related product "Producto Relacionado" had to be Samsung Galaxy S5 and not LG Flex (attached image) that happens in every post with the view I always obtain LD Flex which is the latest product created in the blog.

Where is the problem?

Thanks

#211621

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

You need to add the filter "Post relationship - Post is a child of" to make it working well.

#211821

Thanks Adriano,

I have added the query filter but now the view in the post has vanished

hidden link

#211831

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

I will enable the private information section on this ticket, so please provide credentials of the site. I will need that to debug this issue for you.

#211837

Well,

credentials are

wp-types
adriano.wp-types

Tell me if you need something more.

Thanks

#211905

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

There is a htaccess protection, could you provide it too?

#211912

Oh, I'm so sorry I forgot it

actualidadblog
actualidadblog

If you are banned give me your ip and we will unblock it.

Best regards

#212100

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

I need to see the Related Product section, I think you disabled it. Can you enable it again and explain how to see the issue?

#213049

Sorry Adriano, I was on holidays.

Here the view hidden link

and here a post with the view inserted hidden link

I have removed query filter "is child of" and it is visible again.

Do you need anything more?

Regards

#213241

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

First you need to create a relation between Entradas post type with the desired post type. The example you show me is an "Entrada", what posts are related with it? hidden link

So then you can use the php code below to add some arguments on your view:

add_filter( 'wpv_filter_query', 'posts_same_parent_func', 10, 3 );
function posts_same_parent_func($query, $view_settings, $views_ID){
    if($views_ID == 123)
    {
        $query['meta_key'] = '_wpcf_belongs_empresas_id';
        $query['meta_value'] = get_post_meta($post->ID, '_wpcf_belongs_empresas_id', true);
    }
    return $query;
}

Of course you need to replace 123 with the ID of the View you want to work. Also you should replace "empresas" with the ID of the parent post type.

When using Types posts relationships, we store the parent of a given type in a field _wpcf_belongs_{parent-slug}_id. Assume we are displaying a Car post type that usually has a parent Maker post type. Each Car will store its Maker id in the field _wpcf_belongs_maker_id.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

#216569

Hi again Adriano and sorry for the delay. We have been testing the code you gave me and doing other actions, but the problem is still there.

After Reading your last answer I would like to explain you the structure of the relationships between our types again.

First at all, Type "Entradas" is the default WordPress "Posts" Entradas is only the translation into spanish

Therefore, we have a Parent called Empresas with two childs Smartphones and Tablets. Each child type Smartphones and Tablets have assigned as child Posts = Entradas.

In the settings of each Type we have setting the relationships (Fig 1 attached) and when we write a post, talking for example about a Smartphone we relate this post with his parent, e.g. HTC ONE M8 (Fig 2 attached)

Am I doing it right?

At last, what we want to get is to show information about parent in this selected posts inserting the "Productos Relacinados" view.

Perhaps I am wrong and there are another easier solution. What I want is "as simple as" show parent information into Posts.

#216933

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

I think the configuration of your relationship is not correct. The correct one should be:

Smartphones: Its parent should be "Empresas" and Children you should let blank
Tablets: Its parent should be "Empresas" and Children you should let blank

If you still can not configure it properly I can help you, the private area is still enabled, you could safely share credentials of your site by there.

#218196

Hi again,

I have done the changes you suggested me but I can't get it work

I let you the access to wp-admin and remember we have .htaccess protection actualidadblog actualidadblog

If you can let it working I will learn how it is done for other projects.

Thanks in advance

#218321

Adriano
Supporter

Languages: English (English )

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

Dear Miguel,

The configuration of the post types are fine. The thing is you will need to customize the query of the View to achieve what you want, which is to list all posts of the same parent than the current displayed. So the code below should does this for you, please add in functions.php of the current theme:

add_filter('wpv_filter_query', 'same_parent_func', 10, 2);
function same_parent_func($query, $settings) {
    if($settings['view_id'] = 123)
    {        
        $query['meta_query'][] = array(
        'key' => '_wpcf_belongs_post_id',
        'value' => get_post_meta(get_the_ID(), '_wpcf_belongs_post_id', true),
        'compare' => '='
        );
    }
    return $query;
}

You need to replace 123 with the ID of the view, also you will need to replace "_wpcf_belongs_post_id" with the correct post parent slug, I think it is "_wpcf_belongs_empresa_id".

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

#223991

Hi,

we have done the changes you suggested but we have the same problem, the view return always a wrong result

The topic ‘[Closed] Problems to show parent fields into posts with a view’ is closed to new replies.