Is it possible to add if statments inside php sql query
-
Hi I am trying to create a query where the user has the option to choose ordeby post_count or by registerdate, i’ve created sql query but my problem i cannot add if statmente inside the query so that if input field postcount empty orderby registration ASC/DESC, else if not empty orderby postcount ASC or DESC, I have tried to add the iff inside the sql query but s not working i’ve tried to concatenate sql query but not sure if i did right based on wordpress prepare stament it doesnt work. can someone please help me out below my query
$users = ''; $users = $wpdb->get_results( $wpdb->prepare( "SELECT {$wpdb->users}.ID, p.post_count, display_name, user_registered FROM {$wpdb->users} LEFT JOIN ( SELECT post_author, COUNT(*) AS post_count FROM {$wpdb->posts} WHERE post_type = 'advert' GROUP BY post_author ) p ON {$wpdb->users}.id = p.post_author WHERE user_registered between '2020/01/01' and '$data_final_format'; if (!empty($registro_asc_desc)) { $users.= 'ORDER BY user_registered DESC'; } else if (!empty($post_asc_desc)) { $users.= 'ORDER BY p.post_count DESC'; } user_registered LIMIT 20", $post_type ) );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Is it possible to add if statments inside php sql query’ is closed to new replies.