How to extend WP_Query to include custom table in query?

Important disclaimer: the proper way to do this is NOT to modify your table structure, but to use wp_usermeta. Then you will not need to create any custom SQL to query your posts (though you’ll still need some custom SQL to get a list of everyone that reports to a particular supervisor – in the … Read more

How to print the excuted sql right after its execution

The $wpdb object has some properties getting set for that: global $wpdb; // Print last SQL query string echo $wpdb->last_query; // Print last SQL query result echo $wpdb->last_result; // Print last SQL query Error echo $wpdb->last_error; Note: First of all you have to set define( ‘SAVEQUERIES’, true ); in your wp-config.php file at root folder … Read more

Exclude post ID from wp_query

I suppose this was heavy, but to answer your original question, I’ve collected all of the posts id’s in an array in the first loop, and excluded those posts from the second loop using ‘post__not_in’ which expects an array of post id’s <?php $args1 = array(‘category_name’ => ‘test-cat-1’, ‘order’ => ‘ASC’); $q1 = new WP_query($args); … Read more

Pagination when using wp_query?

Replace <!– WHAT GOES HERE?????? –> with the pagination code below: <div class=”pagination”> <?php echo paginate_links( array( ‘base’ => str_replace( 999999999, ‘%#%’, esc_url( get_pagenum_link( 999999999 ) ) ), ‘total’ => $query->max_num_pages, ‘current’ => max( 1, get_query_var( ‘paged’ ) ), ‘format’ => ‘?paged=%#%’, ‘show_all’ => false, ‘type’ => ‘plain’, ‘end_size’ => 2, ‘mid_size’ => 1, ‘prev_next’ … Read more

meta_query with meta values as serialize arrays

No, it is not possible, and could even be dangerous. Serialised data is an attack vector, and a major performance issue. I strongly recommend you unserialise your data and modify your save routine. Something similar to this should convert your data to the new format: $args = array( ‘post_type’ => ‘my-post-type’, ‘meta_key’ => ‘_coordinates’, ‘posts_per_page’ … Read more

How to get post id of static front page?

WordPress has a few useful options. You can get the homepage ID by using the following: $frontpage_id = get_option( ‘page_on_front’ ); or the blog ID by using: $blog_id = get_option( ‘page_for_posts’ ); Here’s a list of many useful get_option parameters.

How to get an array of post data from wp_query result?

You should read the function reference for WP_Query on the WordPress codex. There you have a lot of examples to look at. If you don’t want to loop over the result set using a while, you could get all posts returned by the query with the WP_Query in the property posts. For example $query = … Read more

tech