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
Related
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(); ?>
I'm trying to create a menu based on higher hierarchy posts from a custom post type.
The thing is, that I can't find the way to filter hierarchy with get_posts function.
This is what I have so far...
<?php
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'pb_progproy',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = get_posts( $args );
foreach( $posts as $post ){
?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
I know that this will give me all posts regardless of it's hierarchy. What I need is just the ones with higher hierarchy.
Any ideas?
Let's figure this posts structure..
Post 1
Post A
Post B
Post 2
I just want Post 1 and Post 2 to be returned from get_posts function. Is there a way?
If you're using the parent-child hierarchy, you can get parent posts filtering posts where "post_parent = 0"
<?php
$args = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'pb_progproy',
'post_parent' => 0
'post_status' => 'publish',
'suppress_filters' => true
);
$posts = get_posts( $args );
You can read more about post filters here.
Im creating a AJAX feature that needs to search posts.
Currently returning all like this works fine...
$the_query = new WP_Query(
array (
'post_type' => 'post',
'posts_per_page' => '10',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
)
);
foreach($the_query->posts as $post):
echo $post->post_title;
endforeach;
However when i add search withs parameter like below...
$the_query = new WP_Query(
array (
's' => 'mysearch',
'post_type' => 'post',
'posts_per_page' => '10',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
)
);
foreach($the_query->posts as $post):
echo $post->post_title;
endforeach;
I then get an error like
Fatal error: Call to undefined function is_user_logged_in() in /wordpress/wp-includes/query.php on line 2084
Which is the core WP files, im not sure im doing search correctly? Will i have to use a like sql statement to get this? Or am i not doing it correctly?! This is for displaying it front end (non logged in / logged in users)
Many thanks in advance!
I got the same problem.
`$args = array(
'post_type' => 'block',
'posts_per_page' => -1
);
$get_all_block_posts = new WP_Query($args);`
Then I tried:
` $args = array(
'post_type' => 'block',
'posts_per_page' => -1
);
$get_all_block_posts = get_posts($args);`
I am a little confused as to that the issue is here, I am trying to pick out a specific product by doing this:
<?php
ini_set('max_execution_time', 0); //I saw maximum execution time error on your image - this is for that
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
$product_query = new WP_Query( $args );
?>
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); global $product; ?>
<?php the_title(); ?>
<?php echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt ); ?>
<?php endwhile; ?>
But nothing is being produced. So I am creating a product under the relevant category and placing content into the description in both main and product short description but still nothing shows?
The error is probably 'meta_value' => 'yes',. You need to specify a meta_key as well.
$args = array(
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => 'my_meta_key',
'meta_value' => 'yes',
'posts_per_page' => 10,
'product_cat' => 'grammar'
);
I have no idea what the meta key should be so change my_meta_key into what you want. Also make sure that the the value for product_cat is correct. It should be the slug of the category.
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'
);