Storing query variable and then using wp_reset_query

wp_reset_query(); resets the query to the original main query of the page. Therefore, storing the query in another variable is redundant and unnecessary. Updated code: $args = array( ‘cat’ => 15, ‘paged’ => $paged ); $custom_query = new WP_Query( $args ); while ($custom_query -> have_posts()) : $custom_query -> the_post(); endwhile; my_pagination(); //call function wp_reset_query(); //#3. … Read more

How to query for posts without a post_format

You have to query post that has no post format attached: $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘post_format’, ‘field’ => ‘slug’, ‘operator’ => ‘NOT IN’, ‘terms’ => get_terms(‘post_format’) ) ) ); To improve performance you can hardcoding the post format instead of using get_terms to retrieve them: $args = … Read more

How to reduce the number of queries?

Try this: global $wpdb; $num_cat = 6; $query = “SELECT tt.term_taxonomy_id, t.name, COUNT( t.name ), GROUP_CONCAT( p.ID ) , GROUP_CONCAT(m.meta_value) FROM {$wpdb->prefix}term_taxonomy tt NATURAL JOIN {$wpdb->prefix}terms t NATURAL JOIN {$wpdb->prefix}term_relationships tr JOIN {$wpdb->prefix}posts p ON ( tr.object_id = ID ) JOIN {$wpdb->prefix}postmeta m ON ( post_id = ID ) WHERE taxonomy = ‘category’ AND post_type=”post” … Read more

WPDB query – decrypting DB data

It’s just serialized, in WordPress, you can run maybe_unserialize and get back the variable/array. $crosssells = $wpdb->get_results( “SELECT * FROM $wpdb->postmeta WHERE _crosssell_ids <> ” ” ); $array = maybe_unserialize($crosssells); However, there are built in functions to retrieve posts based on meta information. You can use get_posts to retrieve the posts and get_post_meta to retrieve … Read more

Two different conditions with a single WP_Query

So from my understanding: IF > User not Logged In – Show all ‘Public’ (meta) posts. ELSE IF > User Logged In – Show all ‘Public’ (meta) posts (from all users) AND all ‘Private’ (meta) posts from the current logged in user. I’ve run two queries below and combined the posts. It’s not without flaws, … Read more

Set if condition with wp_nav_menu

No need to use conditional here to hide your nav menu when it is empty. Have a look at wp_nav_menu( $args );. It accepts the following arguments $defaults = array( ‘theme_location’ => ”, ‘menu’ => ”, ‘container’ => ‘div’, ‘container_class’ => ”, ‘container_id’ => ”, ‘menu_class’ => ‘menu’, ‘menu_id’ => ”, ‘echo’ => true, ‘fallback_cb’ … Read more

Get data of all posts of a query before pagination

If you want to accomplish it in a single query, hook pre_get_posts to query for all posts: function wpd_archive_all_posts( $query ){ if( $query->is_post_type_archive( ‘attractions’ ) && $query->is_main_query() ) $query->set( ‘posts_per_page’, -1 ); } add_action( ‘pre_get_posts’, ‘wpd_archive_all_posts’ ); Then paginate those results manually in the template: if( have_posts() ){ $posts_per_page = 10; $paged = ( get_query_var(‘paged’) … Read more

Single meta_query query using OR instead of AND in request’s WHERE statement

I believe the shortest and best approach is to (1) modify the query: function search_faqs_metadata($query) { if(!is_admin() && $query->is_main_query()) { if($query->is_search) { $query->set(‘meta_query’, array(array( ‘key’ => ‘_faqs’, ‘value’ => $query->query_vars[‘s’], ‘compare’ => ‘LIKE’ ))); } } } add_action(‘pre_get_posts’, ‘search_faqs_metadata’); and then (2) replace the AND with OR via the get_meta_sql filter: function change_meta_key_where_from_and_to_or($sql) { if(is_search()) … Read more