Hi Toolset,
I have 2 post types EAN and Product. I have a custom field EAN | Id and Product | EAN. Both have a example value like this 5704092105906 and i would like to show all Product EAN posts on a EAN post based on a match between these fields. Is this possible?
Thank you,
Menno
Hi Menno,
Thank you for contacting us and I'd be happy to assist.
This is possible and you can create a new post view for the post type "Product".
In the view's "Query Filter" section, you can include a custom field filter for the "Product | EAN" field, so that it is equal to the shortcode attribute "ean".
( ref: https://toolset.com/documentation/user-guides/views/filtering-views-by-custom-fields/ )
Example screenshot:
hidden link
Note: You can set the comparison type to "number" if the expected values in these custom fields consist of only numbers and "string" if it can contain alphabets as well.
After that, you can call this view on a single "EAN" page, with the current EAN post's value for the "EAN | Id" field, passed as an attribute.
( ref: https://toolset.com/documentation/user-guides/passing-arguments-to-views/ )
[wpv-view name="view-of-the-slug" ean="[types field='ean-field-slug'][/types]"]
Note: You'll replace "view-of-the-slug" with the slug of your new view and "ean-field-slug" with the slug of the field "EAN | Id".
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar,
Thanks for this! I'm almost there.
How do i get this [wpv-view name="view-of-the-slug" ean="[types field='ean-field-slug'][/types]"] in PHP? I'm using a custom php single item page.
I'm using this for calling a view, but how do i add the ean part?
<?php
$singleitempricecomparetop3 = array(
'title' => 'Single | Item | Price compare | Top 3',
'pagetitle' => $post->post_title
);
echo render_view( $singleitempricecomparetop3 );
?>
Thanks for writing back.
If you're using "render_view" function to call the view's output, you can pass the target EAN field value as a parameter, the same way as you're passing "title" and "pagetitle".
( ref: https://toolset.com/documentation/programmer-reference/views-api/#render_view )
For example, you can first get the value of that field, in a new variable using the "Types Fields API" and then pass it as the attribute in the array "$singleitempricecomparetop3":
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )
$target_ean_value = types_render_field( "ean-field-slug", array( ) );
$singleitempricecomparetop3 = array(
'title' => 'Single | Item | Price compare | Top 3',
'pagetitle' => $post->post_title,
'ean' => $target_ean_value
);
echo render_view( $singleitempricecomparetop3 );
Note: Alternatively, you can also call the output of shortcodes in PHP code using "do_shortcode" function:
https://developer.wordpress.org/reference/functions/do_shortcode/
Hi Waqar,
Thanks for your response. I solved it like this:
<?php $item_ean = get_post_meta(get_the_ID(), 'wpcf-item-ean', true); ?>
<div class="col-6 col-xl-3 col-lg-3 px-2 mb-3">
<!-- Product -->
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'wpcf-product-ean',
'meta_value' => $item_ean,
'meta_compare' => '='
);
$advertiser_product_list = new WP_Query( $args );
if ( $advertiser_product_list->have_posts() ) :
?>
<?php while( $advertiser_product_list->have_posts() ) : $advertiser_product_list->the_post() ?>
...
Hi Menno,
Thanks for the update and yes using the WordPress query directly without a view is another way of achieving this.
You're welcome to start a new ticket for a different question or concern and mark this one as resolved.
regards,
Waqar