WP_Query Filtred by author name ( Return null )

That’s not how WP_Query works, it isn’t what the documentation says either. WP_Query is not a function. foreach needs an array, or something that can be iterated on, but you’ve given it a WP_Query object. Instead, look at the documentation or tutorials, all of them follow this basic pattern for a standard post loop: $args … Read more

Should close $wpdb via $wpdb->close()

The close() function was added for completeness in the database abstraction. If the database is prematurely closed in a plugin or theme (as you are using it), the next call to wpdb::query() will re-open the connection. Your Commands out of sync error is a MySQL error, not a WordPress one, and it might be coming … Read more

Ordering Custom Post Types with WP_Query

Here’s a plugin that uses the posts_orderby filter to support a custom _post_type__in orderby in WP_Query that orders the post types by their array placements: <?php /** * Plugin Name: Support for _post_type__in orderby in WP_Query * Plugin URI: https://wordpress.stackexchange.com/a/392910/26350 */ add_filter ( ‘posts_orderby’, function ( $orderby, \WP_Query $q ) use ( $wpdb ) { … Read more

if statement on database query

On your if(), it should be like this: if (!($wpdb->get_row(“SELECT voter_ip FROM ” . $wpdb->prefix . “voter_ips WHERE post_id = $post_id AND voter_ip = ‘$voter_ip'”) Notice the quotation mark added on $voter_ip.

How can I query posts from multiple roles?

Just do the get_users() twice and merge the results: <?php $friends = get_users( array( ‘role’ => ‘friends’ ) ); $enemies = get_users( array( ‘role’ => ‘enemies’ ) ); $friends_n_foe = array_merge($friends, $enemies); $IDs = array(); foreach( $friends_n_foe as $person ) { $IDs[] = $person->ID; } $news = new WP_Query( array( ‘author’ => implode( ‘,’, $IDs … Read more

Meta_query with or without value

check if the $GET[‘rating’] is set ( isset($_GET[‘rating’]) or has any value. ( !empty($_GET[‘rating’]) if not, build the query fecthing all post without ‘meta_query’ for example: if ( (isset($_GET[‘rating’]) && !empty($_GET[‘rating’]) ) { // restrict $meta[] = array(( ‘key’ => ‘rating’, ‘value’ => $_GET[‘rating’] ); } if ( (isset($_GET[‘tax’]) && !empty($_GET[‘tax’]) ) { $meta[] = … Read more

Pagination doesn’t work in custom page template [duplicate]

Try the following and see how you go.. <?php global $paged; global $wp_query; $temp = $wp_query; $wp_query = null; $wp_query = new WP_Query(); $wp_query->query(‘posts_per_page=10&post_type=your_post_type’.’&paged=’.$paged); while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <!– do your loop output here, title, content etc –> <?php endwhile; ?> <?php previous_posts_link(‘&laquo; Newer’) ?> <?php next_posts_link(‘Older &raquo;’) ?> <?php $wp_query = null; $wp_query … Read more