Someone made this code for me to load the posts in json format but it just loads the 10 latest posts. How do I change it to load all the posts?
I tried by?page=2 but it doesn't work.
json url
code
<?php
header("Content-type: application/json");
include ('wp-load.php');
$loop = new WP_Query(array( 'post_status' => 'publish', 'post_type' => 'post'));
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
$posts[] = array(
'id' => $post->ID,
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'guid' => $post->guid,
'image' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
'cats' => the_category_ID( false ),
'post_date' => $post->post_date,
);
endwhile; endif;
echo json_encode($posts);
?>
You should have taken a look at docs. There's a pre-set posts_per_page param which is to be set to -1 if you want all the posts:
$loop = new WP_Query(
array( 'post_status' => 'publish', 'post_type' => 'post', 'posts_per_page' => -1 )
);
Related
To display some items from a post type, I'm using this WordPress query:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input 'year' to get the year of the project.
Thanks.
You can use wp_query like Below
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
Please try this way. Hope is useful.
Thanks
You can use Date Parameters check below code.
$posts = get_posts( array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC',
'date_query' => array(
array( 'year' => 'yourinputyear' )
)
) );
i need to display post from a custom post type but from one specific category, i use the code belowe but show me all post from all categories not just from 7.
<?php
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo "TEST TEST TEST TEST";
echo the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
This is not a valid argument for WP Query.
cat=7
Please read here about taxonomy queries, they should do what you require:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Currently your $args array simply holds an element that is cat=7, which is an incorrect way to pass arguments to the WP_Query constructor. Note that the issue is a little more obvious if you format your $args array with whitespace:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat=7' , /* Here is the problem */
'taxonomy' => 'tourcat'
);
I believe your $args array should look like the following:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat' => 7,
'taxonomy' => 'tourcat'
);
This,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
Should instead be like,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'tax_query' => array( array( 'taxonomy' => 'tourcat','field' => 'ID','terms' => '7' ) ));
Try this :
$postData = new WP_Query(array(
'post_type' => 'tour', // custom post type
'posts_per_page'=>10,
'tax_query' => array(
array(
'taxonomy' => 'tourcat', //custom taxonomy name
'field' => 'id',
'terms' => 7
)
)
));
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "TEST TEST TEST TEST";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;
I have edited my theme on archive.php for show only a specific post and exclude a post with specific meta_key:
$posts_to_exclude_args = array(
'meta_key' => 'vip_box',
);
$posts_to_exclude = new WP_Query( $posts_to_exclude_args );
$to_exclude = array();
while ( $posts_to_exclude->have_posts() ) : $posts_to_exclude->the_post();
$to_exclude[] = $post->ID;
endwhile;
wp_reset_postdata();
$lastupdated_args = array(
'post__not_in' => $to_exclude,
'author__in' => $author,
'category__in' => $terms,
'posts_per_page' => 12,
'has_archive' => true,
'paged' => $paged,
);
$lastupdated_loop = new WP_Query( $lastupdated_args );
query_posts( $lastupdated_args );
<?php if ( have_posts() ) : ?>
And its perfect but now if i open a date link/url mywebsite.com/2017/06 it show all post and not only post in this date, why?.
Please can you help me?
I have fixed with this code, but i think there is a best solution than this. =)
$year = get_query_var('year');
$monthnum = get_query_var('monthnum');
$day = get_query_var('day');
'date_query' => array(
array(
'year' => $year,
'monthnum' => $monthnum,
'day' => $day,
),
),
);
I have a problem on using meta_query in WordPress. the meta_key that iv'e tried is wpcargo_status and the value is Delivered. The problem is it is still getting the other status. This is what iv'e tried...
$wpc_report_args = array(
'post_type' => 'shipment',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'status',
'value' => 'Delivered',
'type' => 'CHAR',
'compare' => '=',
)
),
);
$the_query = new WP_Query( $wpc_report_args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_ID().'<br />';
echo get_post_meta(get_the_ID(), 'shipper_name', true).'<br />';
echo get_post_meta(get_the_ID(), 'status', true).'<br />';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
On my database
Output of my query
Do you have any idea what is wrong with my code?
Updated
I already tried this and it is working but I need multiple meta_query
'meta_key' => 'status',
'meta_value' => 'Delivered',
'meta_compare' => '=',
Try this
$wpc_report_args = array(
'post_type' => 'shipment',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'Delivered',
'compare' => '='
)
)
);
$the_query = new WP_Query( $wpc_report_args );
Type - Default value is 'CHAR'
Iv'e seen that there is no errors on your WP_Query. And there is a possible conflict on your query or there is something overriding on it.
Deactivate other plugins
Theme conflict
Check the parse_query - This hook will execute after WP_Query.
$wpc_report_args = array(
'post_type' => 'shipment',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'wpcargo_status',
'value' => 'Delivered',
'type' => 'CHAR',
'compare' => '=',
)
),
);
$the_query = new WP_Query( $wpc_report_args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_ID().'<br />';
echo get_post_meta(get_the_ID(), 'shipper_name', true).'<br />';
echo get_post_meta(get_the_ID(), 'status', true).'<br />';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Can't get the pagnation to work on the custom post types category page. It works when displaying the custom archive page. When I click on the pagnation it shows the posts from the first page but the URL says page=2.
This is the code i'm using in the archive-slug.php. How can I customize it to work with the taxonomy-slug.php?
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'post_type' => 'dropshippers', 'paged' => $paged ) );
$loop = new WP_Query( array( 'post_type' => 'dropshippers', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 8 ) );
if(have_posts()) : while(have_posts()) : the_post();
//Posts
endwhile; endif;
if(function_exists('wp_pagenavi')) {
wp_pagenavi( array( 'query' => $loop ) );
} else {
echo "No posts";
}
You need to add the name of you category in your query, i prefer to use get_posts for that:
<?php $args = array(
'posts_per_page' => 8,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'dropshippers',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Fullfit this two line with your args:
'category' => '',
'category_name' => '',