Better wordpress attachment query than this

I don’t have the data to test so the code below may be wrong. add_filter(‘posts_search’, ‘set_is_tax_to_true’ ,10,2); function set_is_tax_to_true($search,$query){ $query->is_tax = true; } $args = array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, ‘post_status’ => ‘pending’, ‘post_parent’ => $post_id, ‘order’ => ‘ASC’, ‘orderby’ => ‘ID’, ‘meta_query’=> array( array( ‘key’ => ‘cp_sys_expire_date’, ‘value’ => current_time(‘mysql’), ‘compare’ => … Read more

Exclude all sticky posts front page twenty twelve

Don’t use query_posts. Use a filter on pre_get_posts. function no_front_sticky_wpse_98680($qry) { if (is_front_page()) { $qry->set(‘post__not_in’,get_option( ‘sticky_posts’ )); } } add_action(‘pre_get_posts’,’no_front_sticky_wpse_98680′); By running query_posts you clobber the main query, over-writing it with another query. That is why you break pagination. The new query gets out of sync with what should be the main query.

Showing specific post in order of array wp_query

For anyone struggling with this. ‘orderby’=>’post__in’ makes this work. <?php $successful_musics_array = array(‘1682’, ‘1684’, ‘556’, ‘1793’, ‘558’, ‘1788’, ‘320’, ‘1190’, ‘1865’, ‘1125’, ‘1863’, ‘1540’); $query = new WP_Query( array( ‘post_type’ => ‘songs’, ‘posts_per_page’ => ’15’, ‘post__in’ => $successful_musics_array, ‘orderby’=>’post__in’ ) ); if ( $query->have_posts() ) : ?> <?php while ( $query->have_posts() ) : $query->the_post(); ?> … Read more

Check return value of get_posts

From previous questions, I know you would want optimize your queries. There is no need to change WP_Query into get_posts for perfomance. As I stated in a another post, yes, get_posts() is faster than WP_Query because get_posts() legally breaks pagination, but what get_posts() do can be done with WP_Query. To really understand what get_posts() does, … Read more

Historic WordPress database connection

WordPress still has mysql_query() in its codebase (as of February 27, 2018). mysql_*() was deprecated in PHP 5.5.0 and removed in PHP 7.0.0. Since WordPress still supports PHP 5.2 (for reasons beyond understanding), it needs to support both mysql_*() and mysqli_*(), WordPress will use mysqli_query() when available ( PHP > 5.3.0 ) and fallback to … Read more

I can’t figure out what’s wrong with this statement. $wpdb->query update

Without an error message, it is difficult to tell the exact problem. It is important to debug within WordPress and check your webserver error logs. One problem is your call of prepare(). The method takes 2 or more arguments, you only passed 1. Instead try the following $update_status = $wpdb->query($wpdb->prepare( “UPDATE {$wpdb->prefix}wcpv_commissions SET commission_status = … Read more

Which is the correct way to conditionally enqueue a CSS file?

I agreed with @PatJ — you should check if get_queried_object() returns an object or not. But you could simplify your code by simply using is_category() and in_category() which are conditional tags in WordPress: if ( is_category( 3 ) || in_category( ‘arta’ ) ) { wp_enqueue_style( ‘twentytwenty-style-2’, get_stylesheet_directory_uri() . ‘/style-arta.css’, array(), $theme_version ); }

How to setup a query to output posts by groups of five?

you can create a simple counter and condition: <?php $count = 0; ?> <?php if( $query->have_posts() ) : ?> <ul class=”posts”> <?php while ( $query->have_posts() ) : $query->the_post(); if ($count < 5){ ?> <li> <?php the_title(); ?> </li> <?php }else{ echo ‘</ul><ul class=”posts”>’; ?> <li> <?php the_title(); ?> </li> <?php $count = 0; } $count … Read more