Shortcode – Regex – SQL problem
-
I am creating a shortcode that displays a list of custom post type based on letter parameter. like [affiliate-mall letter=’a’] will display all letter A.
Problem is , how can I set the default/no parameter ex: [affiliate-mall], to query any entries having title that starts with a number or any symbol. I know this is done using regex but I don’t have an idea how to implement this. any hints?THanks! Code below
function custom_shortcode( $atts ) { // Attributes extract( shortcode_atts( array( 'letter' => '' ), $atts ) ); $finalArgs = array ( 'posts_per_page'=> 200, 'order' => 'ASC', 'post_type' => 'Affiliate' ); global $wpdb; // Create a new instance $searchAffiliate = new WP_Query( $finalArgs ); $mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$letter."%' "); $args = array( 'post__in'=> $mypostids, 'post_type'=>'Affiliate', 'orderby'=>'title', 'order'=>'asc' ); $res = new WP_Query($args); $count = 0; while( $res->have_posts() ) : $res->the_post(); global $post; $count++; $client_logo = get_post_meta( $post->ID, '_cmb_affiliate_logo_image', true ); $affiliate_link = esc_url(get_post_meta( $post->ID, '_cmb_affiliate_link', true )); if($count % 4 === 0){ $output .= '<div class="one-fourth" style="padding-left:0;"><a href="' . $affiliate_link . '" target="_blank"><img src="' . $client_logo . '" /></a></div>'; }else{ $output .= '<div class="one-fourth"><a href="' . $affiliate_link . '" target="_blank"><img src="' . $client_logo . '" /></a></div>'; } endwhile; if(!$output){ $finalOutput = "No Entries for Letter $letter found"; }else{ $finalOutput = '<div style="clear:both;float:left;width:100%;">' . $output . '</div>'; } wp_reset_query(); return $finalOutput; } add_shortcode( 'affiliate-mall', 'custom_shortcode' ); ?>
- The topic ‘Shortcode – Regex – SQL problem’ is closed to new replies.