This is a bad habit i've been using in wordpress and i want to stop. I repeat the loop/query code when i should fetch all at once. Here is an example...
<?php
$args = array(
'p' => 9345, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
$args = array(
'p' => 9341, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content(); endwhile;
?>
If i want to display the content of the id 9345 first and the content of the id 9341 second, how do i do this all without duplicating WP_Query? I know about order_by but what if i want to specifically order results from the query?
You can use post__in, Read more
<?php
$args = array(
'post__in' => array(9345, 9341),
'post_type' => 'page'
)
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
//DONT FORGET THIS
wp_reset_postdata();
?>
Related
Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.
im trying to display only children posts, i don't need pagination. In theory this code should query all posts and in second query get posts that have parent. But in reality it displaying all posts with parent.
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args1 = [
'post_type' => 'drama',
'post_parent' => 0, // Only return top level pages
'nopaging' => true, // Alias of posts_per_page => -1, Get all top level pages
'fields' => 'ids' // Only get pages ID's for performance
];
$exclude_parents = get_posts( $args1 );
// Now we can run our query as normal
$args = [
'post_type' => 'drama',
'post__not_in' => $excluse_parents, // Exclude parent pages
'paged' => $paged,
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $parent_p = wp_get_post_parent_id( $post_ID ); ?>
<?php if ( 0 == 0 ) { ?>
<div class="col-md-12"><span><?php the_title(); ?></span></div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
If you are looking for an algo to get those posts which have no child post then the algo should be
Step 1 : get all post id in an array(post_id_arr).
Step 2: get all post's parent id in another array(parent_id_arr);
Step 3: run a loop for parent_id_arr and remove the parent ids from post_id_arr.
Step 4: Now the post_id_arr will have only child post ids ,
I have two custom post types for my site, but am using the same Categories/Tags for both.
I am trying to create a category.php page that will allow me to show all items within that category, but in two separate areas, one for each type.
I can get the first post type to display in the main loop, but how can I structure a second loop to display only the posts in the second type that are of that category?
I suggest you use to instances of WP_Query and in args section, for both sections have set post_type= for the desired one.
<?php
//general code to get the current category id
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
//for second loop of second post type
$args1 = array(
'post_type' => 'your_post_type',
'posts_per_page' => 5,
'cat' => $cat_id,
);
$the_query1 = new WP_Query( $args1 );
if ( $the_query1->have_posts() ) :
while ( $the_query1->have_posts() ) : $the_query1->the_post();
echo get_the_title();
echo get_the_excerpt();
endwhile;
wp_reset_postdata();
I need PHP-code for WordPress (Woocommerce) to display random product link.
For example, I have Product1 and want to display on this page (in description of my product):
"See also other products: [Product2 - link] and [Product3 - link]"
Don't know how, I just need php code to insert it in post/pages/products and everywhere I want on my site.
I'm not a coder and I found this code, for example, to display page title with link, but it's not what I need
<?php
echo ''.get_the_title($product_id).'';
?>
But how to get random product, don't know, thanks for help.
Try this :
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata();
The Perfect solution for outputting a single random product which can be achieved using the following code.
<?php
$post_array=array();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($post_array,get_the_ID());
endwhile;
$random_key = array_rand($post_array, 1);
echo ''.get_the_title($post_array[$random_key]).'';
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Have tested the same for you. It worked. Lemme Know if the same worked for you too.
i have next loop:
<?php woocommerce_product_loop_start(); ?>
<?php
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
From some reason that's display always the same 4 products. what i want is - that on every refresh it will display differents products from all categories i have on WooCommerce.
What i need to add to the loop, or should i need to create something else for that?
it might be a conflict with plugin. Some plugins disable ability for random ordering unless you filter it out with: remove_all_filters('posts_orderby');.
So try to put that before your query:
<?php
remove_all_filters('posts_orderby');
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );