how to limit the number posts per page - wordpress

In creating my theme, I want to show one sticky post in a loop but unfortunately, all sticky posts (there are 5) are displaying. I just want to show 1 or two but I am unable to do so through my coding.
I don't know what I am missing or what I am doing wrong.
<?php
$query = new WP_Query(array(
'post_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'paged' => $paged,
));
?>

To get the last sticky post:
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
query_posts( $args );
if ( $sticky[0] ) {
// insert here your stuff...
}

Related

Render child page on parent page in Wordpress

How can I render a child page on the parent page in wordpress? I'am building a landing page website, and the idea is to use child pages to make landing page structure.
Now I'am using this code in my parent page template:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'rand',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
var_dump($child->posts);
But it just gives me an array, and I need fully rendered HTML of my child pages.
Thank you in advance)
Try this code here:
$args = array(
'hierarchical' => 0,
'child_of' => $post->ID,
'parent' => $post->ID,
'sort_column' => 'menu_order, ID',
);
$pages = get_pages( $args );
foreach ( $pages as $post ) : setup_postdata( $post );
// child page html content here
endforeach;
//reset to the main page
wp_reset_postdata();
Finally I've found the proper solution, inspired by Matt Browne's answer
functions.php
function eatwings_show_page($pageid)
{
global $post;
$post = get_page($pageid);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
parent-page-template.php:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'menu_order, ID',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
foreach($child->posts as $childpage)
{
eatwings_show_page($childpage->ID);
}

How to get all products with their variation in random order in WooCommerce

I have a WooCommerce shop that holds variable products and I want to show all products with their variation products as well as in shop page.
My code is given below:
$params = array('posts_per_page' => 2, 'post_type' => 'product', 'orderby' => 'rand');
$wc_query = new WP_Query($params);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 2,
'orderby' => 'rand'
));
if (have_posts()) :
while (have_posts()) : the_post();
echo '<h2>'.the_title.'</h2>';
if ($product->is_type( 'variable' ))
{
$available_variations = $product->get_available_variations();
foreach ($available_variations as $key => $value){
if($value['variation_is_active']==1){
// varible products details
}
}
}
endwhile;
the_posts_pagination();
wp_reset_postdata();
else:
echo 'No Products';
endif:
// I am getting products but only randomise main product not variation
Please help me.
Thanks in advance
To get variable product you have to use product_variation in
post_type key.
So your $params and query_posts should look like this:
$params = array(
'posts_per_page' => 2,
'post_type' => array('product', 'product_variation'), // <-- check this line.
'orderby' =>
'rand'
);
//...
//...
query_posts(array(
'post_type' => array('product', 'product_variation'), // <-- check this line.
'paged' => $paged,
'posts_per_page' => 2,
'orderby' => 'rand'
)
);
Reference: Get List of All WooCommerce Products
Hope this helps!

Get 5 random posts within current category in WordPress

I want to only get 5 posts within current category by random using the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
How do you apply the '5 posts' limit and 'order by random' to the above code?
as for me, I would use get_posts(), but these arguments should work in your case as well:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
More about this here: https://codex.wordpress.org/Template_Tags/get_posts
I used the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );

WordPress loop ignore first post of three separate meta_values

I have a loop that is pulling all of the news out, however there is three main stories set by ACF. These are Main, Secondary and Third. This wouldn't be a problem if there was only one post set to each field. However, the client wants to be able to just set a new Main post without having to worry about removing the old ones.
So to make that work I'm trying to get the loop to ignore the first of these three fields, while showing the rest AND the other posts that are set to 'No'.
I'm trying something like this but I just cannot see how else to do it.
$args = array(
// 'offset' => 1,
'posts_per_page' => -1,
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Secondary',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Third',
'compare' => 'NOT',
)
),
'meta_query' => array(
array(
'offset' => 1,
'key' => 'main_story',
'value' => 'Main',
'compare' => 'NOT',
)
),
);
I know offset removes the ability to paginate which is important, but I saw https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination and also was told a way to go around this. This part is more important for the time being.
Here's how I finally got around not being able to do the above
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php
$excluded_key = "main_story";
$excluded_val = array("Main", "Secondary", "Third");
$exclude_ids = array();
?>
<?php
foreach ($excluded_val as $exclude) {
$args = array(
'posts_per_page' => 1,
'order' => 'DESC',
'meta_query' => array(
array(
'key' => $excluded_key,
'value' => $exclude,
)
)
);
$excluded_id = get_posts($args);
foreach($excluded_id as $to_exclude) {
$exclude_ids[] = $to_exclude->ID;
}
}
?>
<?php
$args = array(
'post__not_in' => $exclude_ids,
'paged' => $paged
);
?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

Exclude Current and Sticky Post

On a single Post page I have a side bar displaying up to three other, related posts. How can I exclude both Sticky Posts and the Current post?
I know how to exclude the Current post and how to exclude Sticky Posts by using post_not_in in a WP_Query, see code example below. But I guess you can not use post__not_in twice in the same query. Any suggestions?
$current_post_ID = get_the_ID();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' )
'post__not_in' => array($current_post_ID)
);
The post__not_in is an array() and uses post ids (numbers only).
There is no need to use it twice, use it like that:
//First build the array of excluded posts
$excluded_posts = array_push(get_option( 'sticky_posts' ) , $current_post_ID);
//Then use it in the option
'post__not_in' => array( $excluded_posts )
Note the array_push() PHP function, this will add the current post to the end of the sticky posts array, which is then passed to 'post__not_in' option.
<?php
$sticky =array(get_option('sticky_posts'));
// (add post id on sticky_posts option like ex. 485,458,256)
$current_post_ID = get_the_ID();
array_push($sticky,$current_post_ID);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'post__not_in' => array($sticky)
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
<!-- add your code which to display hear -->
<?php
endwhile;
wp_reset_query();
?>

Resources