Navigation überspringen

[Gelöst] passing field values from one view to another view

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

I aim to pass a value from a custom field "product id" on a custom post type template "brands" to another view displaying fields from a different custom post type "products." However, I am uncertain about creating a custom shortcode to achieve this.

Solution:

You're on the right track. Utilize code similar to the example provided in the Toolset forum (https://toolset.com/forums/topic/how-to-pass-a-variable-to-a-view/#post-380479) as a starting point. Additionally, refer to Toolset documentation for adding custom code (https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code) and passing arguments to views via shortcodes (https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/).

Relevant Documentation:

Toolset forum example: https://toolset.com/forums/topic/how-to-pass-a-variable-to-a-view/#post-380479
Toolset documentation:

Adding custom code: https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code
Passing arguments to views: https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/

This support ticket is created vor 2 years, 5 months. 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.

Dieses Thema enthält 3 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Christopher Amirian vor 2 years, 5 months.

Assistiert von: Christopher Amirian.

Author
Artikel
#2686983

On my custom post type template "brands" I would like to pass the value of a field "product id" into a view that will display fields from a different custom post type "products"

i.e. lets say the first view returns the product id value of "42". I would then like the next view to display all the products that have a brand product id of "42"

So I am assuming I have to create a CUSTOM SHORTCODE for the "product id" value and then use that SHORTCODE in the query for the second view. It is this part that I am not sure of how to proceed.

First, am I doing this the right way? Second, the shortcode ... how do I form the function that goes in the theme > functions page

thx

#2687141

Christopher Amirian
Unterstützer

Sprachen: Englisch (English )

Hi there,

The idea is correct, you will need to use a code like this:

https://toolset.com/forums/topic/how-to-pass-a-variable-to-a-view/#post-380479

We will not be able to give you the exact code but I think the code above will give you the starting point

Here is the documentation for adding code to Toolset:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

For the second view that you want to use you can call it via a shortcode and pass the value as a shortcode attribute. For more information:

https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/

Thanks.

#2687493

ok ... so I used the Shortcode Generator and came up with this:

add_shortcode( 'ac-type-code', 'get_aircraft_type_code' );
function get_aircraft_type_code( $atts ) {

// Attributes
$atts = shortcode_atts(
array(
'aircraft-type-code' => '',
),
$atts
);

$actype = wp_get_post_terms( $atts['aircraft-type-code']);
return $actype;

}

when I try to echo the output it just says "Array" ... which I assume is because I need to echo the [0] element of the array.

so, what am I doing incorrect? it should output something like "EMB"

thx

#2687617

Christopher Amirian
Unterstützer

Hi there,

As mentioned we will not be able to troubleshoot custom code as it is outside of our support scope.

Please check the documentation for "wp_get_post_terms " that you used which is a standard WordPress function:

https://developer.wordpress.org/reference/functions/wp_get_post_terms/

It returns an array of objects and not a string.

So you need to loop over the results and if needed get the first entry, something like this:

    // Check if terms were found
    if ( ! empty( $actype ) && ! is_wp_error( $actype ) ) {
        // Get the first term object from the array
        $actype = $actype[0];

        // Return the term name
        return $actype->name;
    } else {
        // If no terms were found, return an empty string or an error message
        return 'No matching term found';
    }

Just to make sure we are on the same page, the sample code above is not meant to be used as a copy/paste, it is just to give you an idea of how to go forward.

If you need further assistance with custom coding you can consider hiring a developer:

https://toolset.com/contractors/

Thank you

#2688000

ok ... I will take a stab at figuring this out. Thank you for your help, I'll create a new ticket at a later date if required.