Get attachment by slug

An attachment is just a post with the post_status = inherit and the post_type = attachment, so can be queried with WP_Query or get_posts. Note that slug (post_name) is unique per post type. $_header = get_posts(‘post_type=attachment&name=book-site-header&posts_per_page=1&post_status=inherit’); $header = $_header ? array_pop($_header) : null; $header_url = $header ? wp_get_attachment_url($header->ID) : ”; you can also use the … Read more

Query to return maximum of one post per author

You need to GROUP BY the author ID, which is going to require a filter on posts_groupby. The Codex page for that filter is non-existent but it works like posts_join. Something like… function filter_authors($groupby) { global $wpdb; $groupby = ” {$wpdb->posts}.post_author”; return $groupby; } add_filter(‘posts_groupby’,’filter_authors’); $args = array( ‘showposts’ => 3, ‘author’ => “1,2,3” ); … Read more

Make First post different from rest?

You could check current_post in the loop: if($queryObject->have_posts()) { while($queryObject->have_posts()) { $queryObject->the_post(); if( 0 == $queryObject->current_post ) { // this is the first post } else { // not the first post } } }

Why is my WP_Query not working when tax_query terms are an array?

When you’re doing a tax_query or meta_query in a WP_Query, you always have to use a nested array( array() ); just see the following example for an explanation and pay attention to the relation argument. $packages = new WP_Query( array( ‘post_type’ => ‘vamos-cpt-packages’, ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘vamos-holiday-types’, ‘field’ => … Read more

WP_Query with meta_value LIKE ‘something%’

For a left-sided match you can get around the automatic adding of ‘%’‘s by WP by using regular expression RLIKE: ‘meta_value’ => ‘^’ . preg_quote( $today ), ‘meta_compare’ => ‘RLIKE’ Similarly for right-sided match: ‘meta_value’ => preg_quote( $today ) . ‘$’, ‘meta_compare’ => ‘RLIKE’

Getting an array out of WPQuery

Your function returns $product->get_id();, instead of that, you should save those values into an array and at the end return that array. function ids(){ $args = array( ‘numberposts’ => -1, ‘post_type’ => ‘product’, ‘meta_key’ => ‘wppl_is_dax’, ‘meta_value’ => ‘1’ ); // query $the_query = new WP_Query( $args ); $allIds = array(); if( $the_query->have_posts() ): while( … Read more

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