How to loop through ALL pages?
I was able to learn how to do it. Here’s a solution: $pages = get_pages(); foreach($pages as $page) { echo($page->post_content); }
I was able to learn how to do it. Here’s a solution: $pages = get_pages(); foreach($pages as $page) { echo($page->post_content); }
Try this to get the count of posts: <?php $connected = new WP_Query( array( ‘connected_type’ => ‘posts_to_pages’, ‘connected_items’ => get_queried_object(), ‘nopaging’ => true, ) ); echo “<h2>Found: $connected->found_posts</h2>”; ?> You could write a variable like: $the_count = $connected->found_posts; Then test with an if if ( ( $connected->have_posts() ) && ( $the_count > 1 ) ) … Read more
Likely this is happening because you are using a custom page template. Try the following. I’ve commented the steps along the way. Hope it helps. <?php //get the current page $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; //pagination fixes prior to loop $temp = $query; $query = null; //custom loop using WP_Query $query = new … Read more
You need to filter the main WordPress query using the pre_get_posts action. This code will get you started: function show_only_private_post_for_logged_in_users( $query ) { if ( ! $query->is_main_query() || is_admin() ) { return; } if ( is_user_logged_in() ) { $query->set( ‘post_status’, ‘private’ ); } } add_action( ‘pre_get_posts’, ‘show_only_private_post_for_logged_in_users’ ); It will only affect the main query … Read more
You could do this pretty easily with a simple query: global $wpdb; $results = $wpdb->get_results(“SELECT * FROM $wpdb->postmeta WHERE meta_key = ‘_wp_attachment_metadata’ AND meta_value LIKE ‘%carousel%'”); foreach($results as $result){ //$result->post_id will give you the original attachment post }
You only need the if ( have_posts() ) : if, as the name of the function suggets, you need to do something different if you don’t have posts. This would be something like displaying a “No posts found.” message. But you’d only need that on templates that could show no posts, like archives and search. … Read more
The posts are still held in memory under WordPress’ cache mechanism (even though you replace $posts on every loop) – delete each one after operating on it: # do some echoing with the $post # wipe post from memory wp_cache_delete( $post->ID, ‘posts’ ); wp_cache_delete( $post->ID, ‘post_meta’ ); Pro tip: save some needless queries with no_found_rows … Read more
When WordPress include the category.php (just like any other template) the main query (i.e. the query triggered by the url) is already ran: it means that a query on database was already fired and this is the reason why in a template you can run the Loop without calling a query. If you call get_posts … Read more
Run a conditional check in the foreach loop using is_category($term-name) Assign a class variable to active if it’s the same as $term->name $terms = get_terms( ‘category’ ); echo ‘<ul>’; foreach ( $terms as $term ) { $class = ( is_category( $term->name ) ) ? ‘active’ : ”; // assign this class if we’re on the … Read more
Add wp_reset_query(); after your loop to prevent other loops on the page (for example, the navigation) to break. <?php query_posts( array ( ‘category_name’ => ‘left_sidebar’, ‘posts_per_page’ => 4, ‘orderby=menu_order’ ) ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <li><a href=”https://wordpress.stackexchange.com/questions/84336/<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php endwhile; endif; wp_reset_query(); // reset … Read more