php - Dynamic function with attributes in shortcode -


i have below function counts number of posts meta key(in example warranty field) set 1 , taxonomy(dealership-tax grimsby selected) - able change meta key , taxonomy term via shortcode can reuse function different fields. have looked codex , read $atts few tests have not been successful. advice appreciated, thanks!

    function counttheposts_func( $atts ) {         $args = array(             'posts_per_page'    => -1,             'post_type'         => 'deal',             'post_status'       => 'publish',             'meta_key'          => 'wpcf-warranty',             'meta_value'        => '1',           'tax_query' => array(               'relation' => 'and',               array(                   'taxonomy' => 'dealership-tax',                   'field' => 'slug',                   'terms' => array('grimsby'),               ),           )         );          $posts_query = new wp_query($args);         $the_count = $posts_query->post_count;          echo $the_count;       }      add_shortcode( 'counttheposts', 'counttheposts_func' ); 

you try (query not tested):

function counttheposts_func( $atts ) {     /**      * defaults      *      * @var array      */     $args = shortcode_atts( array(         'posts_per_page' => -1,         'post_type'      => 'deal',         'post_status'    => 'publish',         'meta_key'       => 'wpcf-warranty',         'meta_value'     => '1',         'tax_query'      => array(             'relation' => 'and',             array(                 'taxonomy' => 'dealership-tax',                 'field'    => 'slug',                 'terms'    => array( 'grimsby' ), // slug should result of sanitize_title().             ),         ),     ), $atts );      /**      * if shortcode contains 'terms' attribute, should override      * 'tax_query' since have treat differently.      */     if ( isset( $atts['terms'] ) && '' !== $atts['terms'] ) {         /**          * can try multiple term slugs separating them ",". you'll          * want escape each term sanitize_title() or          * prevent empty results, since expects slug, not title.          *          * @var array          */         $terms = explode( ',', $atts['terms'] );          $args['tax_query'] = array(             'relation' => 'and',             array(                 'taxonomy' => 'dealership-tax',                 'field'    => 'slug',                 'terms'    => $terms,             ),         );     }      $posts_query = new wp_query( $args );      return $posts_query->post_count; } 

the raw result of $args variable if shortcode called [counttheposts terms="term1,term2" meta_key="my-meta-key"] is:

array (     [posts_per_page] => -1     [post_type] => deal     [post_status] => publish     [meta_key] => my-meta-key     [meta_value] => 1     [tax_query] => array     (         [relation] => ,         [0] => array         (             [taxonomy] => dealership-tax             [field] => slug             [terms] => array             (                 [0] => term1                 [1] => term2             )         )     ) ) 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -