Why WP_Query in functions.php is not working when get_posts works?
Why WP_Query in functions.php is not working when get_posts works?
Why WP_Query in functions.php is not working when get_posts works?
Untested, but I think you’d need to do two separate queries and merge them together: $args = array( ‘post_type’ => ‘featured-posts’ ); $featured_posts = get_posts($args); $args = array( ‘post_type’ => ‘review-posts’, ‘category_name’ => ‘featured-category’ ); $review_posts = get_posts($args); $result = array_merge($featured_posts->posts, $review_posts->posts); $final_query = WP_Query($result);
You forget $query->the_post(), which makes the same as setup_postdata(). Also you are using $post->ID inside the loop but $post is not current post object in the loop. You have also syntax errors; for example you pass thumnail to wp_get_attachment_image() instead of a string “thumbnail”, and pass name as orderby argument instead of the string “name”, … Read more
query_posts() is overly simplistic and a problematic way to modify the main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like … Read more
Recently I have developed a theme almost identical to yours. This is how you can achieve this: Basic idea is, create two different layouts: layout-three.php and layout-two.php We will call them based on requirement. I have used an array to achieve this. Below is the sample code for the template file (assuming you will show … Read more
The get_query_var() function is looking at the main query for the page, which you are not actually using here. You’re using a custom query. So, try looking at what is in $result instead.
get_post returns a WP_Post object, so you can retrieve the post parent with $post->post_parent. You should have a look at the member variables in the WP_Post link.
The problem here is not that the posts that are being wrongly displayed are displayed under the wrong category, it’s that they’re not actually in the right category. For example, the post “Supporting healthy eating choices” is displayed under “O” as it has the category “O”. Going to your backend and assigning the proper category … Read more
You can use the transition_post_status action to manipulate the post content at the time when it is published. You just need to add a function like the one below into your theme or plugin: function post_published( $new_status, $old_status, $post ) { if ( $new_status == ‘publish’ && $old_status != $new_status) { //Do whatever you want … Read more
You need to use WP_Query. get_posts will only get you post =) <?php $user_id = get_current_user_id(); echo $user_id; $args=array( ‘post_type’ => ‘page’, ‘post_status’ => ‘published’, ‘posts_per_page’ => 1, ‘author’ => $user_id ); $wp_query = new WP_Query($args); while ( have_posts() ) : the_post(); the_title(); endwhile; ?> Tested, and it is working