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(); ?>
Related
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.
I am new to wordpress, I have a post that has a category_name of offer and it has a content, here is the permalink : http://localhost/jcjohn/2016/09/20/what-we-offer/
Now I want to display the contents of my post from my section page.
Here is my code inside the section :
<section id = "offer">
<?php
$args = array(
'type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_post()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
So here is what you would need to do to display the post on the single page. You seem to have missed the s from have_post so it needs to be like below
Note: This would go inside index.php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
<?php
if ( $offerBlog->have_posts() ) :
while ( $offerBlog->have_posts() ) : $offerBlog->the_post();
the_content();
endwhile;
else : ?>
<div class="no-content">
<h3>Well... it looks like we forgot to put content here.</h3>
</div>
<?php
endif;
wp_reset_postdata();
?>
</section>
You try using this loop with the cat_id that you have for the category you create.
query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );
You can have a try of the replaced code as follows.
<section id = "offer">
<?php
$args = array(
'post_type' => 'post',
'cat' => '1',
'posts_per_page' => 5,
'orderby' => 'title',
'order' => 'DESC'
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_posts()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
You have missed the loops format.
Reference:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
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>
I need to display separately the first and second most recent posts from a category without using a loop. Below is what I've tried; the item1 div should display the most recent and the item2 div should display the second most recent.
<div class = 'item item1'>
<?php get_posts('numberposts=1&offset=1&category='); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;?>
</div>
<div class = 'item item2'>
<?php get_posts('numberposts=2&offset=1&category='); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;?>
</div>
There is a built-in function for this called: wp_get_recent_posts()
With the following params:
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
Example:
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" ><h3>' . $recent["post_title"].'</h3></a></div>';
}
?>
Source: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
You can use get_posts to retrieve two posts and put them in an array to use wherever you like.
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 ?>