How to pass orderby params to $wpdb->prepare()?

You don’t need to use $wpdb->prepare() for ORDER BY clauses. $wpdb->prepare() will always quote your variables. Supposing you receive the ordering in the request, you can prevent SQL injection by not using the user entered value at all: $sql = “SELECT….”; if ( ‘asc’ == $_GET[‘order’] ) { $sql .= ‘ ORDER BY p.post_date ASC’; … Read more

Get posts from multiple post type

So you want to query posts from multiple post types, but also with different query-arguments like custom field, tax_query or categories. As you did not post what you tried, have already or want to have, please try something like this: We are using 3 queries to set everything up. You can add different arguments to … Read more

wp_query not working with post_type

First I would state that in most cases the query_posts should be avoided! This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. I would suggest transfer your query with WP_Query … Read more

Sort posts in loop by the WooCommerce Membership of the author

I would flag post as member_post when it is submitted to database and use this flag to sort them. function wpse_287048_flag_member_post( $post_id ) { if ( wc_memberships_get_user_active_memberships() !== false ) { // Check if user is member update_post_meta( $post_id, ‘member_post’, 1 ); } else { update_post_meta( $post_id, ‘member_post’, 0 ); } } add_action( ‘save_post’, ‘wpse_287048_flag_member_post’ … Read more

Why is my WP Query not returning first result’s post meta?

$show_date = get_field(‘show_date’); $show_venue = get_field(‘show_venue’); // print ‘<pre>’; // print_r($show_date); // print ‘</pre>’; $query->the_post(); You are setting the active post AFTER getting the values. Put $query->the_post() always at the top of that while loop, so that the other functions can rely on the global $post variable being set correctly.

Can I display custom post types in home.php or need page template?

The simple answer is “yes, you can”, but I’m pretty sure it won’t satisfy you 😉 So… How can you achieve this and show only custom posts on home.php? All you need is to use pre_get_posts hook like so: add_action( ‘pre_get_posts’, ‘add_custom_post_types_to_home_query’ ); function add_custom_post_types_to_home_query( $query ) { if ( is_home() && $query->is_main_query() ) $query->set( … Read more

how to get post order by post id wp_query?

OK, so you want to define posts order by yourself. WP_Query allows you to do that – you’ll have to use orderby => post__in to achieve it. And that’s what you do. So why isn’t it working? Because of a typo 😉 Ordering posts by post__in preserves post ID order given in the ‘post__in’ array. … Read more

How to access custom WordPress database tables

Are you sure that’s the table name? Usually on install WordPress prompts you to setup a table prefix so the default isn’t wp_. Additionally, you need to call the global $wpdb object so it’s accessible to use: global $wpdb; $user_count = $wpdb->get_var( “SELECT COUNT(*) FROM $wpdb->users” ); echo “<p>User count is {$user_count}</p>”; Finally, to prevent … Read more

Proper way to display latest 5 posts grouped by post type?

<?php $post_types = array( ‘bmw’ => ‘BMW’, // post type to use in query => post type to show in <h2> ‘audi’ => ‘Audi’ ); foreach( $post_types as $post_type => $post_type_name ): ?> <h2>Latest <?php echo $post_type_name; ?> Posts:</h2> <ul> <?php $args = array( ‘numberposts’ => 5, ‘post_type’ => $post_type ); $recent_posts = wp_get_recent_posts( $args … Read more