0

I'm learning how to pass parameters in shorcode, I know the basic step after read through WP Codex.

But for now, I have plugin come with custom fields and as the title, I want to know how to do it.

Here is the code from plugin that adds custom fields

$prefix = '_al_listing_';
$fields = [];

$fields[] = [
    'name' => __( 'Price', 'auto-listings' ),
    'id'   => $prefix . 'price',
    'type' => 'number',
    'min'  => 0,
    'step' => '0.01',
];
$fields[] = [
    'name' => __( 'Suffix', 'auto-listings' ),
    'desc' => __( 'Optional text after price.', 'auto-listings' ),
    'id'   => $prefix . 'price_suffix',
    'type' => 'text',
];

$fields = apply_filters( 'auto_listings_metabox_details', $fields );

ksort( $fields );

return [
    'id'         => $prefix . 'details',
    'title'      => __( 'Details', 'auto-listings' ),
    'post_types' => 'auto-listing',
    'fields'     => $fields,
    'context'    => 'side',
];

And here is the code I built to call shortcode with parameters, but I only know pass 1 custom fields at one time.

public function listings( $atts ) {
    $atts = shortcode_atts(
        [
            'orderby' => 'date',
            'order'   => 'asc',
            'number'  => '20',
            'price' => ''
        ],
        $atts
    );

    $query_args = [
        'post_type'           => $post-type,
        'post_status'         => 'publish',
        'meta_key'            => '_al_listing_price',
        'orderby'             => 'meta_value',
        'meta_value' => $atts['price'],
        'order'               => $atts['order'],                
        'posts_per_page'      => $atts['number'],
    ];

    return $this->listing_loop( $query_args, $atts, 'listings' );
}

Any helps are appreciated.

0

You can do this several different ways. Need to know how you are passing the 1 custom field in your current shortcode.

It seems like you might be doing it like this:

[your_short_code price="how_are_you_setting_this"]

if this is the case then you can just add more attributes like this:

[your_short_code price="how_are_you_setting_this" next_field="set_this_field"]

// From your code above with adjustments.
public function listings( $atts ) {
global $post;
// You can add on to the default atts.
    $atts = shortcode_atts(
        [
            'orderby' => 'date',
            'order'   => 'asc',
            'number'  => '20',
            'price' => '',
            'next_field' =>'',
        ],
        $atts
    );

// Not exactly sure what you are trying to do here.
    $query_args = [
        'post_type'           => $post->post_type,
        'post_status'         => 'publish',
        'meta_key'            => '_al_listing_price',
        'orderby'             => 'meta_value',
        'meta_value'          => $atts['price'],
        'order'               => $atts['order'],                
        'posts_per_page'      => $atts['number'],
    ];

    return $this->listing_loop( $query_args, $atts, 'listings' );
}

Another way of doing it is like this:

// notice that the attribute fields has multiple fields separated by commas.
[your_short_code fields="how_are_you_setting_this,set_next_field,and_set_next"]

If you go with the separated commas then in your shortcode on the php side you will need to do as follows:

// From your code above with adjustments.
    public function listings( $atts ) {
    global $post;
    // You can add on to the default atts.
        $atts = shortcode_atts(
            [
                'orderby' => 'date',
                'order'   => 'asc',
                'number'  => '20',
                'fields' => '',
            ],
            $atts
        );

        // This will make an array out of your comma separated fields.
        $fields = explode( ',', $atts['fields'] );

    // Not exactly sure what you are trying to do here.
        $query_args = [
            'post_type'           => $post->post_type,
            'post_status'         => 'publish',
            'meta_key'            => '_al_listing_price',
            'orderby'             => 'meta_value',
            'meta_value'          => $atts['price'],
            'order'               => $atts['order'],                
            'posts_per_page'      => $atts['number'],
        ];

        return $this->listing_loop( $query_args, $atts, 'listings' );
    }

Hope this helps you.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.