I want to get latest post based on author loop.
this is my code:
function display_artist_func() {
$out_args = array(
'post_type' => 'product',
'author' => 1,
'showposts' => 3
);
$out_loop = new WP_Query($out_args);
while ($out_loop->have_posts()) :
?>
<div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>
<?php
//$product = wc_get_product( $product->id );
$args = array(
'post_type' => 'product',
'showposts' => 10,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;$author = get_user_by( 'id', $product->post->post_author ); ?>
<li>
<?php
if ( has_post_thumbnail( $loop->post->ID ) )
echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' );
else
echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="65px" height="115px" />';
?>
<h3><?php the_title(); ?></h3>
<h4><?php echo $author->display_name;?></h4>
<div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>
<?php
//echo $product->get_price_html();
//woocommerce_template_loop_add_to_cart( $loop->post, $product );
?>
</li>
<?php
endwhile;
endwhile;
wp_reset_query();
}
add_shortcode( 'display_artist', 'display_artist_func' );
there is 2 loop:
outside loop get the 3 author.
inside loop get the 3 post based on author from outside loop.
in outside loop a want to display author avatar.
but this code not work.
Your problem is that you're never setting up the post data in your outer loop. It looks to me like this code creates an endless loop? The function get_the_author_email() is actually deprecated, but what its doing is using global $authordata.
In your inner loop, you call:
$loop->the_post();
which is going to take your current query, setup postdata for the active post and then remove it so it no longer returns in your call to have_posts(). You're missing a call to:
$out_loop->the_post();
And so your postdata isn't setup, and you're going to create an endless loop.
NOW with all that said
To be perfectly honest your code doesn't look like it makes a lot of sense. Your 'outer loop' is querying products with one specific hard-coded author ID, but the only thing that outer post is used for IS to get the author email. The way its written - the author is going to be the same for every run of $out_loop.
So I think you still may be missing some logic here - I don't know from your description exactly how its supposed to work, but as is - if you're just looping through products, there's no need for two loops here.
Related
Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>
I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>
I'm using a custom post type, and pulling that information throughout the site via a loop. It seems unnecessary to create a loop every time I want to call information from the custom post type. Is there another way?
Here's an example of what the loop looks like, and how I call it.
<?php
$args = 'post_type=post-custom&order=ASC';
query_posts($args);
if ( have_posts($args) ) : while ( have_posts() ) : the_post();
$my_meta = get_post_meta($post->ID,'_my_meta',TRUE);
?>
<img src="<?php echo $my_meta['img'];?>">
<?php
endwhile;
endif;
?>
Any help is appreciated!
You need to use some kind of loop...If you don't want to use The Loop, you can use a standard foreach loop and get_posts(). Because you're only interested in getting post meta, you can use the fields argument to return an array of post IDs, instead of full post objects:
$args = array(
'post_type' => 'post-custom',
'order' => 'ASC',
'posts_per_page' => -1,
'fields' => 'ids'
);
$post_ids = get_posts( $args );
foreach ( $post_ids as $id ) {
$meta = get_post_meta( $id, '_my_meta', true );
echo '<img src="' . $meta['img'] . '">';
}
As a final note, you should basically never use query_posts().
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!