Sort in WP_Query(), not filter? Is it possible?

If you want to sort the posts by the meta post_views_count, and still include posts that do not have that meta, you can use meta_query like so: ‘meta_query’ => array( ‘relation’ => ‘OR’, // make sure it’s OR // Include posts that have the meta. array( ‘key’ => ‘post_views_count’, ‘compare’ => ‘EXISTS’, ), // Include … Read more

Dynamically Override Fancy Title – Part II

I finally got it. The final version of the function is: <?php // ACCOMODATION TITLE add_filter (‘presscore_get_page_title’,’netbooking_presscore_get_page_title’,10,1); function netbooking_presscore_get_page_title ( $title ) { if( is_page( ‘1057’ ) ){ $GLOBALS[“content”] = $content; // This reads input considering a default value in case of error $INPUT = function($param, $default, $prefix = true){ $key = $prefix ? ‘netbooking_structure_’.$param … Read more

Adding session variable and/or cookie based on user-selected input

You can definitly use PHP session variables. Follow this blog post (“Listing 3”) for the best way to enable PHP sessions in WordPress. Namely, you need to use this code in your plugin or in your theme’s funtions.php: add_action( ‘init’, ‘session_start’, 0 ); After that you can use basic session variables to set the country … Read more

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