0

I am using advanced custom fields and my problem is: I have several campers for a different number of persons. Now I need a shortcode to display all campers for all certain numbers of persons. The shortcode should be like this [shortcode_name, personens="x"]. The x stands for the different number of persons for which the function should filter the campers.

My starting code is the following (shows all campers for 6 persons):

function modell_nach_personenzahl_variabel() { 

    $args = array(
        'post_type'   => 'womos',
        'order'       => 'ASC',
        'orderby'     => 'personen',
        'field'    => $atts['personen'],
        'numberposts' => -1,
        'meta_query' => array (
            array (
                'key' => 'personen',
                'value' => '6'
            )
        )
    );

Thanks for the help.

1

1 Answer 1

0

This is not the full working code, just to illustrate how to add the shortcode and trigger the function.

<?php

   // Check if ACF is available
   if(function_exists('get_field')) {
       function modell_nach_personenzahl_variabel() {
           $args = array(
               'post_type'   => 'womos',
               'order'       => 'ASC',
               'orderby'     => 'personen',
               'field'    => $atts['personen'],
               'numberposts' => -1,
               'meta_query' => array (
                array (
                    'key' => 'personen',
                    'value' => '6'
                )
            )
        );

        $query = new WP_Query($args);

        if($query->have_posts()):
            while($query->have_posts()):

             // for testing purposes
             echo "<pre>";
                print_r($query->the_post);
             echo "</pre>";

             ?>
                <h1><?php the_title(); ?></h1>
                <!-- get the rest of the fields -->

            <?php
            endwhile;

        endif;

       // Add the shortcode
       add_shortcode('filter-camper-nach-personen', 'modell_nach_personenzahl_variabel');
   }
1
  • Thanks for the answer. Now I need the filter for the numer of persons which can be choosen within the shortcode. I'm thankful for every idea.
    – kuh13
    Apr 12, 2019 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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