I'm using this in my page template to get posts by their category:
<?php
if (is_page(19)){
?>
<ul>
<?php
global $post;
$args = array( 'category' => 'Testimonial' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
</ul>
<?php } ?>
but it's retrieving all posts instead. Not just the ones labeled Testimonial. Any idea what I'm doing wrong?
'category_name'=>'this cat' also works but isn't printed in the WP docs
Check here : https://developer.wordpress.org/reference/functions/get_posts/
Note: The category parameter needs to be the ID of the category, and
not the category name.
You can use 'category_name' in parameters.
http://codex.wordpress.org/Template_Tags/get_posts
Note: The category_name parameter needs to be a string, in this case, the category name.
add_shortcode( 'seriesposts', 'series_posts' );
function series_posts( $atts )
{ ob_start();
$myseriesoption = get_option( '_myseries', null );
$type = $myseriesoption;
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post();
echo '<li><a href="';
echo the_permalink();
echo '">';
echo the_title();
echo '</a></li>';
endwhile;
echo '</ul>';
}
wp_reset_query();
return ob_get_clean(); }
//this will generate a shortcode function to be used on your site [seriesposts]
Create a taxonomy field category (field name = post_category) and import it in your template as shown below:
<?php
$categ = get_field('post_category');
$args = array( 'posts_per_page' => 6,
'category_name' => $categ->slug );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
//your code here
<?php endforeach;
wp_reset_postdata();?>
Related
I want to display all members of my site.
As I understand the WooCommerce Membership plugin, the members are stored as custom post type (https://docs.woocommerce.com/document/woocommerce-memberships-data-structure/#section-4) and I could get them with get_posts().
I tried to do so with the following code:
<ul class="my-5">
<?php
global $post;
$args = array( 'post_type' => 'wc_user_membership' );
$posts = get_posts( $args );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
Unfortunately without any result.
Is there anything wrong with my code?
Updated:
Try to add in your $args array 'numberposts' => -1 and 'post_status' => 'wcm-active' so your code will be:
<ul class="my-5">
<?php
$posts = get_posts( array(
'post_type' => 'wc_user_membership',
'post_status' => 'wcm-active',
'numberposts' => -1,
) );
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<li><?php echo $post->ID; ?></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
You don't need global $post; as you are using $post in the foreach loop.
Tested and works.
I'm trying to exclude posts by the author with the ID "1" form WP_Query. This still shows all posts by all users.
Any thoughts?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="grid__third">
...
</div>
<?php endwhile; // end of loop
?>
Thanks a lot!
try this
global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
or
// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();}else {
// no posts found
}
If your query is not working with
$wp_query = new WP_Query( array( 'author' => -1 ) );
then, You can try some other logic. Like inside the loop get the ID of the author by using POST ID:
$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here
Then apply condition like if($post_author_id != 1){ // YOUR POST } This will display post of all author except author with ID = 1.
Full code like:
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
<div class="grid__third">
...
</div>
<?php
} endwhile; // end of loop
Hope this will helpful for you. Thanks.
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>
On single posts i display other posts like this outside the loop
$posts = get_posts('numberposts=200&category='. $category->term_id);
foreach($posts as $post) :
Trying adding
query_posts($query_string . '&orderby=rand');
But however i try it don't do it in random order
Problem was that i didn't put it first in get_posts like
$posts = get_posts('orderby=rand&numberposts=200&category='. $category->term_id);
foreach($posts as $post) :
Try this:
<?php
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php
endforeach;
wp_reset_postdata(); ?>
You can see the codex:
http://codex.wordpress.org/Template_Tags/get_posts
I am using this query for getting event custom posts
<?php
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<p><?php echo get_post_meta($post->ID, 'events_photo', true); ?></p>
<?php endforeach; ?>
Now I want to be create a shortcode for this query. I am using this code
function get_events(){
global $post;
$args = array( 'posts_per_page' => -1, 'post_type'=> 'events' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
$list_events= '';
$event_title= get_the_title();
$event_photo= get_post_meta($post->ID, 'event_photo', true);
$list_events.='
<h2>'.$event_title.'</h2>
<p>'.$event_photo.'</p>
';
$list_events= '';
endforeach;
}
function insert_event($atts, $content=null){
$events_all= get_events();
return $events_all;
}
add_shortcode('all_events', 'insert_event');
but, it is not working. Can anyone help?