Get author if post has multiple authors

The Co-Authors Plus plugin uses it’s own template tags which replace the default WordPress tags, the_author() and the_author_posts_link(). coauthors() – Outputs the co-authors display names, without links to their posts. coauthors_posts_links() – Outputs the co-authors display names, with links to their posts. coauthors_links() – Outputs the co-authors display names, with links to their websites if … Read more

Order by most used tag

I’m afraid orderby taxonomy terms post count is not possible by WP_Query. See this answer. So here I’ve written a SQL based function for you. It’ll order your posts based on terms post count. function the_dramatist_order_posts_by_tag_count( $args=”” ) { global $wpdb; $param = empty($args) ? ‘post_tag’ : $args; $sql = $wpdb->prepare(“SELECT posts.*, terms.name AS tag, … Read more

WP_query : Force last posts number over Reading Settings

In your loop, you must refer to $wp_query instance: <?php $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => 3); $wp_query = new WP_Query($args); if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> <h4><a href=”https://wordpress.stackexchange.com/questions/266621/<?php the_permalink() ?>”><?php the_title(); ?></a></h4> <?php the_excerpt(); endwhile; } else { echo ‘No posts’; } wp_reset_postdata(); ?> Also, the_excerpt() … Read more

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

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

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

why does not my wp_query work?

There are several issues here, starting with the first part: <?php $postnumcat = new wp_query (array( ‘post_status’ => ‘publish’, ‘post_type’ => ‘post’, ‘cat’ => ‘-1’, ‘posts_per_page’ => ‘-1′, )); Firstly, wp_query is misspelt, it’s WP_Query Some general notes too: Setting posts_per_page to -1 is bad for performance. Even if you don’t expect there to be … Read more

wp_query sort with meta_value_num is not working

From your comment: it worked without the array( ‘key’ => ‘picture_url’, ‘value’ => ‘none’, ‘compare’ => ‘!=’ ) but I need to include it in the query… is it possible? Yes, using named sub-meta queries like so: $q_args = [ // … other args here … ‘meta_query’ => [ ‘picture_url’ => [ ‘key’ => ‘picture_url’, … Read more