Get posts by menu ID

I recently needed the same thing, I had menu of categories and I needed to get the categories that in the menu. After a several hours digging in the WP core, I found the wp_get_nav_menu_items() function that helped me. I finnally came with this, 220 was my menu_id $navcat = wp_get_nav_menu_items(‘220’); foreach ($navcat as $obj) … 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

How to paginate attachments in a secondary query as gallery?

I’m assuming you want something like this: |———————| | content | | (static) | |———————| | gallery | | (paged) | |———————| | << pagelinks >> | |———————| In this setup your the post content stays the same, appears to be static, while the gallery is paged. This means you have the main query, showing … Read more

Displaying several specific pages using WP_Query()

If you look at the query object after those queries are run, you’ll see that none of those examples are doing what you think they’re doing. The first issue is that you can’t mix query string and array parameters, and the second issue is that pagename can only be used to load a single page. … 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

WP_Query for WooCommerce Products

<ul class=”products”> <?php $args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => 12 ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) { while ( $loop->have_posts() ) : $loop->the_post(); wc_get_template_part( ‘content’, ‘product’ ); endwhile; } else { echo __( ‘No products found’ ); } wp_reset_postdata(); ?> </ul><!–/.products–>

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