Get all posts without tags

A WP_Query where ‘tax_query’ has all tag terms and operator ‘NOT IN’: $tags = get_terms(‘post_tag’, array(‘fields’=>’ids’) ); $args = array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘post_tag’, ‘field’ => ‘id’, ‘terms’ => $tags, ‘operator’ => ‘NOT IN’ ) ) ); $untagged = new WP_Query( $args );

Get random terms

Unlike a normal WP_Query(), get_terms() or WP_Term_Query() does not have random ordering. You would either need to do it in SQL yourself or grab all the terms and shuffle them, pulling out 6 to make your random term array: // Get all terms $terms = get_terms( array( ‘taxonomy’ => ‘webshops’, ‘hide_empty’ => false, ) ); … Read more

WP_Query class not found

That’s because when calling a theme template directly you only have what’s included/defined in the template file, not the whole WordPress environment. Some people attempt to fix this by adding an include of wp-header or load etc but this is incredibly damaging When doing AJAX requests, never call a file in the theme, instead call … Read more

How can I hide posts that are over 2 years old

You can use the pre_get_posts hook to modify the main query: add_action( ‘pre_get_posts’, ‘filter_old_posts’ ); function filter_old_posts($query){ if( !is_admin() && $query->is_main_query()){ add_filter(‘posts_where’, $callback = function( $where=””){ $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-2 years’)) . “‘”; return $where; }); add_filter(‘getarchives_where’, $callback ); } } This will filter the main query posts to … Read more

wp query by search in titles only & put the posts in loop

If you want to limit the filter from this answer (add that function to your plugin or your theme’s functions.php) just to one query, remove the filter when you are done: add_filter( ‘posts_search’, ‘__search_by_title_only’, 500, 2 ); $query = new WP_Query( array( ‘s’ => ‘mykeyword myotherkeyword’, ‘cat’ => array( 22, 32 ), ‘orders’ => ‘DESC’, … Read more

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