Clean up WordPress code that repeats

To expand on the answer by Global: Paste this into your theme’s functions.php: function get_stations_query( $terms ) { //create new query object $query = new WP_Query( array( ‘post_type’ => ‘stations’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘orderby’ => ‘title’, ‘order’ => ‘ASC’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘genres’, ‘field’ => ‘name’, ‘terms’ => $terms … Read more

Create WP_Query to search for posts by their categories or their parent/child categories

Use get_categories first to get all the post categories to use in the form, then loop through them to display a multicheck input… function hierarchical_category_inputs ($cat, $indent) { $cats = get_categories(‘hide_empty=false&orderby=name&order=ASC&parent=”.$cat); $indent++; if ($cats) { foreach ($cats as $cat) { echo “<input type=”checkbox’ name=”cat-“.$cat->term_id.”””; echo ” style=”margin-left: “.($indent*10).”px;”> “.$cat->name.”<br>”; hierarchical_category_inputs($cat->term_id, $indent); } } } echo … Read more

Two near-identical custom field types – one works, the other doesn’t . What can cause this?

I found an older post showing what I was missing from my code, so it seems like I didn’t do a good enough search to begin with. For anyone wondering I was missing ‘compare’ => ‘LIKE’ from the query, so the code would look like this: $args_post = array(‘post_type’ => ‘release’, ‘orderby’ => ‘_custom’, ‘order’ … Read more

Why get_posts() not returning only selected category posts from Custom Post Type?

First, your custom post type is portfolio-item, not portfolio_item. Note that you register the post type with $this->base name and previously you did $this->base=”portfolio-item”;. Also, numberposts is obsolete, use post_per_page instead: $args = array( // ‘post_type’ => ‘portfolio_item’, ‘post_type’ => ‘portfolio-item’, ‘orderby’ => ‘rand’, ‘post_per_page’ => 1 ); Then, the taxomy argument of your query … Read more

unable to retrive parent page information

As defined by the WordPress codex use this to get the parent get_post_ancestors( $post ) , The direct parent is returned as the first value in the array. Edit: get_post_ancestors( get_queried_object_id() ) can also work WordPress Codex

WordPress add_rewrite_rule() cannot visit lower url levels

I fixed my problem as follows: // Adds rewrite rules for shop post type function gtp_add_shop_rewrite_rules() { add_rewrite_rule( ‘^shop/([^/]*)/([^/]*)/([^/]*)$’, ‘index.php?shop-category=$matches[1]&width=$matches[2]&height=$matches[3]’, ‘top’ ); add_rewrite_rule( ‘^shop/([^/]*)/([^/]*)$’, ‘index.php?shop-category=$matches[1]&width=$matches[2]’, ‘top’ ); add_rewrite_rule( ‘^shop/([^/]*)$’, ‘index.php?shop-category=$matches[1]’, ‘top’ ); } add_action(‘init’, ‘gtp_add_shop_rewrite_rules’, 10, 0);

new WP_Query to get max price meta value not working

As @Milo suggested and also codex documented it is_main_query Under_the_Hood This function is an alias for the method WP_Query::is_main_query(). In filter or action hook callbacks that are passed the WP_Query object, such as pre_get_posts, it is circular to call this function. Instead, directly call the passed object’s method. For example, if your filter callback assigns … Read more