Random Post ruining my Wordpress comments section - php

I currently have this code on my template:
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
The nature of the code is to generate a list of Random post from blog post. The problem is, the code starts ruining my comments section by displaying the wrong list of comments to unrelated blog post.
see my sample on the link above
The common sense to do, is to remove to code in my template. My question is any ideas on how to fix the code above so i can still use it?

In case you work with get_posts, and you need to override the $post, you'll have to do it like this:
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
<?php $post = $tmp_post; // reset the $post to the original ?>

Related

i want to filter category wise post on same page in wordpress

filter category post wise on same page in WordPress if i click category
plastic i want result plastic product
for example product category
1.plastic
2.metallic
3.silver
showing image
enter image description here
post div
<?php
global $post;
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'menu_order',
'order' => 'ASC'
)
);
?>
sidebar div
<?php
global $post;
$curVal = "";
$myposts = get_posts(
array(
'post_type' => 'product',
'numberposts' => '999',
'orderby' => 'product_category',
'order' => 'ASC'
)
);
?>
<ul>
<?php
foreach($myposts as $post){
if($curVal != get_field('franchise_category')) { ?>
<li>
<a href="<?php echo home_url( $wp->request ); ?>?cat=<?php echo get_field('product_category'); ?>">
<?php echo get_field('product_category'); ?>
</a>
</li>
<?php }$curVal = get_field('product_category');} ?>
</ul>
1st if you want to get all posts with this query use:
'numberposts' => -1,// this will return all available, right now you are passing a string 999 - '999'
2nd - you're not setting up your post data and restoring context of the template tags Try this:
foreach ( $myposts as $post ) : setup_postdata( $post );
...
...
endforeach;
wp_reset_postdata();
Edit your code and see the result.

How to get child posts of current custom post and order it by custom field number?

I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>

Get tag posts wordpress

I've written some code which automatically creates some posts and adds a tag to them. I can see the tags in the 'All posts' admin panel and I can click on the posts 'Tag' link to get just those posts with the tags.
Here is my code:
<?php $tag_ID= single_tag_title();
$args = array(
'post_type' => 'post',
'tag_id' => $tag_ID,
'posts_per_page' => 10,
'order' =>'ASC'
);
$posts = get_posts( $args );
var_dump($args );
foreach ( $posts as $post ) {
?>
Can you help me to get all tag posts?
Thank you.
Create a new file (tag.php) in wp-content/themes/yourthemefolder/
and put below code in it.
<?php
get_header();
$tag = single_tag_title('', false);
echo '<h1>Tag: ' . $tag . '</h1>';
$args = array(
'post_type' => 'post',
'taxonomy' => $tag,
'terms' => $tag,
'posts_per_page' => 10,
'order' => 'ASC'
);
$postslist = get_posts($args);
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div id="post">
<h2>Post title:<?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php endforeach;
get_footer(); ?>

WordPress extra categories are showing on blog page

I have following code
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 3,
'cat_id'=> 5,
));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I want to show category whose id is 5 on my bog page. But this is showing other categories as well like 4 & 3.
Similarity issue is for archive page as well. Where i am wrong?
check this, i think this is helpful for you.
<?php
$posts = get_posts('category=5&orderby=rand&numberposts=5');
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php } ?>
Please try with below code :
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
this is working at my end
Try this
<?php
$args3 = array(
'numberposts' => 1,
'cat' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$homepage_post = get_posts($args3);
foreach ($homepage_post as $post) : setup_postdata($post);
// write your code here ...
endforeach;
wp_reset_postdata();
?>
--
Thanks

Avoiding Duplicate item in loop

I wrote a query to show related posts in Wordpress,
A brief explanation:
First it checks what categories current post belongs to,
then puts "slug" name of that categories inside an array ("$cats")
Then using foreach, I query the posts with same taxonomies.
This code works great except for when current post belongs to more than one category and there is another post which belongs to these categories too, in this situation the other post gets displayed twice or even more depending on how many categories they share,
So the question is, How do I check if a post is repeating and how to prevent it?
Code:
<?php
$terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
foreach ($cats as $cat) {
$args=array(
'taxonomy' => $cat ,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
if($currentid !== get_the_ID()){
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php
if ( has_post_thumbnail() ){
$appfetured = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ));
$appfeturedimage = $appfetured[0];
}else{
$appfeturedimage = '../img/defaultappicon.png';
}
?>
<img class="appfeatured" src="<?php echo $appfeturedimage; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
</li>
<?php
}
endwhile;
wp_reset_query(); // Restore global post data stomped by the_post().
}//end foreach
?>
If all you are trying to do is get all of the posts in all of the categories for the current post try this:
<?php
$args = array(
'cat' => wp_get_post_categories($currentid),
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4
);
new WP_Query($args);
It's been a while since I've used Wordpress so I don't know if there's a difference between the categories and the taxonomies so you can try this:
<?php
terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
$args = array(
'taxonomy' => $cats,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);

Categories