Modify main WordPress loop with a parse_query filter

Ultimately all meta queries get run through _get_meta_sql. wp-includes/meta.php Line 402 – 403 in the _get_meta_sql function The following coditional statement runs for any meta queries. if ( empty( $meta_key ) && empty( $meta_value ) ) continue; There’s a ticket here for this which outlines what we should be able to do. http://core.trac.wordpress.org/ticket/16735 What you … Read more

How-to exclude terms from the main query the most performant way?

You can set the taxonomy query for the main query using pre_get_posts: add_action( ‘pre_get_posts’, ‘my_exclude_terms_from_query’ ); function my_exclude_terms_from_query( $query ) { if ( $query->is_main_query() /* && whatever else */ ) { $tax_query = array ( array( ‘taxonomy’ => ‘category’, ‘terms’ => array( ‘cat-slug’ ), ‘field’ => ‘slug’, ‘operator’ => ‘NOT IN’, ) ); $query->set( ‘tax_query’, … Read more

Why doesn’t /2013/01/ properly return January’s archives in archive.php?

Don’t use query_posts in the template for simple modifications of the main query. Use the pre_get_posts action instead to modify the query before it runs: function wpa_date_posts_per_page( $query ) { if ( !is_admin() && $query->is_date() && $query->is_main_query() ) { $query->set( ‘posts_per_page’, 5 ); } } add_action( ‘pre_get_posts’, ‘wpa_date_posts_per_page’ );

get comments and get posts in loop

get_comments accepts an array of arguments, you are passing an integer. If you want to retrieve all comments for a post use: get_comments( array(‘post_id’ => $post->ID, ‘status’ => ‘approve’) ); To get an already formatted comment list, is easier use the wp_list_comments() function, instead of another foreach cycle (code from codex): echo ‘<ol class=”commentlist”>’; //Gather … Read more

pre_get_posts with get_posts

Firstly, you are invoking an infinite loop, which causes the memory exhaustion. To avoid it, put the following at the beginning of your function: // avoid infinite loop remove_action( ‘pre_get_posts’, __FUNCTION__ ); It makes sure the you are not hooking it into pre_get_posts over and over again, re-initiating your get_posts() call over and over again. … Read more

Custom Post Type Pagination Not Working On Archive Page

Modify main query on post type archive page Instead of direct modify main query, just use normal loop ( you can create post type template, in your case archive-portofolio.php ), and run your modifying query under filter pre_get_posts. Your normal loop on archive-portfolio.php <div class=”row text-center small-up-1 medium-up-2 large-up-3 small_section”> <?php if ( have_posts() ) … Read more

Get Custom post with ID

You simply don’t need to specify a post_type when calling get_post(). The ID for any sort of post is unique among the whole DB-posts table. So if you’re calling a post with ID = 17 $id = 17; $post = get_post( $id ); then you’ll simply get this single post. Note, according the Codex, when … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)