Need wp_query to return all children and grandchildren

Ok, I found a solution. I first looped through and got all the child page ID’s, created an array, and used post_parent__in <?php $parentarray = array($parentid); $parentposts = new WP_Query( array( ‘posts_per_page’ => -1, ‘post_type’ => ‘page’, ‘post_parent’ => $parentid ) ); while ( $parentposts->have_posts() ): $parentposts->the_post(); ?> <?php $parentarray[] = get_the_id(); ?> <?php endwhile; … Read more

WordPress Related Post by tags in Single.php

The code shown isn’t working as wished, what is wrong? you’re using only the first tag : $first_tag = $tags[0]->term_id; to grab posts from all tags you have to iterate the whole array until a max of 5. The code below ( untested) grabs a max of 25 posts theoretically using up to 5 tags..BUT … Read more

if statement in wp_query arguments

As said here : Conditional arguments for WP_Query and tax_query depending on if $somevar has a value I can define the args outside of the WP_Query instantiation: <?php $tax_query = array(‘relation’ => ‘AND’); if (isset($search_course_area)) { $tax_query[] = array( ‘taxonomy’ => ‘course-area’, ‘field’ => ‘id’, ‘terms’ => $search_course_area ); } if (isset($search_course_level)) { $tax_query[] = … Read more

How to vary post loop results layout and resume?

How do I get a WP_Query object containing the next five posts? : // $query is the original WP_Query object. $five_posts = new WP_Query(); $five_posts->posts = array_slice( $query->posts, $query->current_position, $query->current_position + 5 ); How do I then elegantly resume the standard-layout loop output at the right place? : /* Sum the count of all the … Read more

Custom query for sidebar isn’t returning results

Based on our chat in the comments here’s what I came with. Please advise if it works. <?php function primary_category_query_shortcode( $post, $atts ) { //global $post; //Uncomment this global if it still doesn’t work. $atts = shortcode_atts( $defaults, $atts, ‘primary_category_query’ ); $my_meta = get_post_meta ( $post->ID, ‘_yoast_wpseo_primary_category’, true ); // WP_Query arguments $args = array … Read more

How to loop for every result found in the_content() when using the search query?

got a solution with https://stackoverflow.com/questions/15737408/php-find-all-occurrences-of-a-substring-in-a-string $lastPos = 0; $positions = array(); if($pos==”){} else{ while (($lastPos = strpos($content, $findme, $lastPos))!== false){ $positions[] = $lastPos; $lastPos = $lastPos + strlen($findme); } // Displays foreach ($positions as $eachPos){ $pos = $eachPos; $exctractAfterQuery = substr($content,($pos+strlen($findme) ),40); $exctractBeforeQuery = substr($content,$pos-40,40); $newContent .= “[…”.$exctractBeforeQuery.'<strong class=”search-highlight”>’.$findme.”</strong>”.$exctractAfterQuery.”…]<br>”; } } It works well and … Read more