Get post/page data outside the loop

You’re using non-existent variables in your function, $post_id, $post, $page. Use get_queried_object to get data from the current page. if( is_single() || is_page() ){ $this_page = get_queried_object(); $excerpt = get_post_field( ‘post_excerpt’, $this_page->ID ); }

Displaying recent post excerpts on static front page

I’ve found the solution. According to https://codex.wordpress.org/Function_Reference/the_content#Overriding_Archive.2FSingle_Page_Behavior, one must add global $more; $more = 0; before the call to the_content(). So now my inner loop reads: <?php $recent_posts_query = new WP_Query(array(‘post_type’ => ‘post’, ‘posts_per_page’ => 5)); while ($recent_posts_query->have_posts()) { $recent_posts_query->the_post(); ?> <div class=”post”> <h3><?php echo the_title(); ?></h3> <p>by <?php the_author(); ?></p> <?php global $more; $more … Read more

How to display all posts from standard posts to custom post types in a loop with pagination?

You don’t need a custom query to include CPTs in homepage posts in normal cases. Just use pre_get_posts. function wpse215208_include_all_cpt( $query ) { if ( is_home() && $query->is_main_query() ) { $query->set(‘post_type’, array( ‘post’, ‘photo’, ‘video’, ‘web’ ) ); } } add_action(‘pre_get_posts’, ‘wpse215208_include_all_cpt’);

Override global query results without hooks

It is quite hard to understand what you need to do here, but you need to look at the following Do not use the global variables as local variables, it breaks the global variables and causes issues with the loop. It is also quite hard to debug when you run into issues. The only global … 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

How to check if a post exist?

You want to rethink this code… to begin with, as the number of entries grow, looping them all is a bad idea. Consider adding a meta_query argument to the WP_Query so you are not looping through every single entry in the future, but limiting the database retrieval instead. Mainly you want to get rid of … Read more

get_posts that match a user-specified value on a page

Using a variable will make your code a little less repetitive: $case_history_code = get_field( ‘case-history-code’ ); if ( $case_history_code ) { // make the code lowercase $case_history_code_lower = strtolower( $case_history_code ); // use the lowercase inside of `get_posts` $myposts = get_posts( ‘numberposts=3&offset=0&category_name=” . $case_history_code_lower ); foreach ($myposts as $post) : setup_postdata($post); ?> <a href=”https://wordpress.stackexchange.com/questions/220489/<?php the_permalink(); … Read more

How can I show custom fields in the loop only to specific user roles?

You can use current_user_can() and is_user_logged_in() to validate current user: <?php if ( is_user_logged_in() && current_user_can(‘role’) ) : ?> <span style=”font-size:16px”> <strong>Currently reading:</strong> <?php the_field(‘book_name’); ?> (<?php the_field(‘book_year’); ?>)<br/> <strong>Currently seeing:</strong> <?php the_field(‘movie_name’); ?>”><br/> </span> <?php endif; ?> Make sure to change role to your preference and take a look at Codex for more info.