Fill up array with pages wordpress - php

I have a page for products on my website and I have an array to store all of the pages that have products as a parent. This works fine but for some reason, it stops filling up the array after 10 entries. This is my code to fill up the array.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any');
// The Query
$parent = new WP_Query( $args );
$n = 0;
// The Loop
if ( $parent->have_posts() ) {
echo '<ul>';
while ( $parent->have_posts() ) {
$parent->the_post();
echo '<li>' . get_the_title() . '</li>';
$n++;
echo $n;
}
echo '</ul>';
// Restore original Post Data
wp_reset_postdata();
}
This returns the titles of the Pages and echo's a number to count them. The output shows the titles but stops after 10 pages, even though I have 12 pages with products as a parent. The strange thing is that when I add another page with products as a parent it pushes the last one in the list out and puts the newly added one in the first spot in the array.
Any help is welcome!

Use posts_per_page instead of numberposts.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'posts_per_page' => -1,
'post_status' => 'any');

You can define how many posts are returned by setting posts_per_page in your $args array.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any',
'posts_per_page' => '100');

Related

Woocommerce - Guys I'm trying to display 3 random products but skipping the first 3 most recent added products

I need your help. I'm trying to display 3 random products but skipping the first 3 most recently added products. Most recent meaning not by query but by global date the product was created.
Heres the code i use to display random products.
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
Adding 'offset' only skips the first 3 random. Is there a way to skip first 3 most recently added products?
First, get three last products and get their IDs using wp_get_recent_posts function and map IDs, then add post__not_in argument to WP_query with these three post IDs
$recent_posts = wp_get_recent_posts([
'post_type' => 'product',
'numberposts' => 3
]);
$last_three_posts = array_map(function($a) { return $a['ID']; }, $recent_posts);
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
'post__not_in' => $last_three_posts,
);
$loop = new WP_Query( $args );

Wordpress short code to display all child page by parents ID

I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here

Limit posts per page after using array_merge in Wordpress

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

Limit posts with attachment inside Wordpress custom post type query

I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}

How to retrieve attachments from child pages of a specific Page in WordPress?

How would I retrieve attachments from all subpages of a specific Page ID?
Example:
SPECIFIC PAGE
Child (with attachments)
Child (with attachments)
Child (with attachments)
I'm currently using this code to retrieve all attachments site-wide, however I would like to limit this to only pull images from all children of a specific Page.
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
Almost there using this code as per Nick's suggestion below:
<?php
$mypages = get_pages('child_of=19');
foreach ( $mypages as $mypage ) {
$attachments = get_children(array('post_parent' => $mypage->ID, 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand'));
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
}
?>
However, there are two remaining issues:
Limiting the amount of total photos pulled. Using 'numberposts' only limits the amount of images pulled from each post
Randomization. Orderby => rand only randomizes the images within each post. I would like to randomly shuffle the order for everything.
Try using get_pages( $args )
<?php $args = array(
'child_of' => 'SPECIFIC PAGE',
'parent' => 'SPECIFIC PAGE',
'post_type' => 'attachment',
'post_status' => 'publish',
'numberposts' => -1
); ?>
Using child_of will get all children and grandchildren.
parent will limit this to just the children that have this as a parent. No grandchildren.
See here for more details. http://codex.wordpress.org/Function_Reference/get_pages

Categories