I am using this code from which I am able to get 3 recent posts.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC'));
and I am using this code to get 3 custom posts with the post__in method.
$query = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => 3, 'post_status' => 'publish', 'order' => 'DESC', 'post__in' => array( 10244, 7177, 8262)));
How can I combine them to get the 3 recent and 3 custom posts from one loop?
Any Help Appreciated. Thanks.
You can do it by merging their posts into one query.
f.e.
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
Example:
<?php
$merged_query = new WP_Query();
$merged_query->posts = array_merge( $query1->posts, $query2->posts );
while ( $merged_query->have_posts() ) : $merged_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
Related
$posts = new WP_Query( [
'post_type' => 'mock_test',
'meta_query' => array(
'key' => 'selectedchapter',
'value' => 15
),
] );
echo "<pre>";
print_r($posts);
echo "</pre>";
I need to get posts which have selected chapter 2 in custom meta box. This WP_Query not working for me, I'm getting all the posts.
see attached screenshot (https://prnt.sc/vuzw6lhW44mR) of metabox html structure.
This is a simple way to get post where the custom field key is 'color' and the custom field value is ‘blue’:
$args = array(
'post_type' => 'product',
'meta_key' => 'color',
'meta_value' => 'blue'
);
$wp_query = new WP_Query( $args );
Try these code
$args = array(
'post_type' => 'mock_test',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_title();
the_excerpt();
$meta_print_value=get_post_meta(get_the_ID(),'selectedchapter',true);
echo $meta_print_value;
endwhile;
I want to get some products of an post_author from DB. The problem is that get_posts is not working properly, from my debug that's a problem from the theme that I am using.
Is there any alternative to get products from DB?
What I tried:
-----1----
$related_products = get_posts( array(
'post_type' => 'product',
'author' => 19,
'post_status' => 'publish',
) );
-----2----
$product_query = new WP_Query( array(
'author' => 19,
'post_type' => 'product',
'post_status' => 'publish',
) );
$related_products = $product_query->posts;
-----3----
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'product',
'author' => 19,
) );
$GLOBALS['wp_query'] = new WP_Query();
$related_products = $GLOBALS['wp_query']->query( $args );
I get results, but random products.
my query doesn't seem to want to function if the post limit set is beyond what it is... i.e. if I have 370 rows of information and I set the posts_per_page to anything beyond that, it won't run the query?!
wp_reset_query();
$args = array(
'post_type' => 'courses',
'posts_per_page' => 370,
'post_status' => 'publish',
);
$loop = new WP_Query( $args );
I've tried using 1000, -1, all sorts, but it only functions if I enter the exact number of rows expected, otherwise the script fails to run at the WP_Query stage so don't have any errors either.
Edit: The actual limit seems to be at 375, after that, nothing executes although there are 386 actual rows that fulfil the criteria.
Any ideas?!!
<?php
$args = array(
'post_type' => 'courses',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 370,
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<?php echo the_content(); ?>
<?php
endwhile;
wp_reset_query();
?>
This MUST work
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'courses',
'post_status' => 'publish',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) :
while($the_query->have_posts()) :
$the_query->the_post(); ?>
YOUR HTML CODE
<?php endwhile; endif; ?>
I had this exact same problem except the limit for my CTP was 411. Anymore more rows and the query didn't work. Came across a few similar posts and one solution was to increase server resources but then I looked for how to improve wp_query performance. Try this:
$args = array(
'post_type' => 'courses',
'posts_per_page' => -1,
'post_status' => 'publish',
'update_post_term_cache' => false, // don't retrieve post terms
'update_post_meta_cache' => false, // don't retrieve post meta
);
It worked for me and seems a good option when displaying a CTP index listing page. Discussed here:
https://drujoopress.wordpress.com/2013/06/27/how-to-optimize-wordpress-query-to-get-results-faster/#more-184
http://codex.wordpress.org/Class_Reference/WP_Query#Caching_Parameters
I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);
How can I skip the first post in WordPress?
<?php
$recentPosts = new WP_Query();
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Use the offset parameter:
<?php
$recentPosts = new WP_Query( 'offset=1' ) );
$recentPosts->query(array('showposts' => 6,'post_type' =>array('stiri')));
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Use the offset
$recentPosts = new WP_Query (
array(
'post_type' => 'stiri',
'post_status' => 'publish',
'posts_per_page' => 6, // all = -1
'orderby' => 'date',
'order' => 'DESC',
'offset' => 1
)
);