I'm having problems to get author_id in this code. Don't show nothing. Some can help me?
Entire code: (I'm trying get custom_post_type value of the same author of post.
<?php global $query_string;
query_posts( $query_string . '&orderby=rand' ); ?>
<?php while(have_posts()): the_post();
$author = get_the_author();
echo 'Autor:'.$author;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
?>
<?php
$blogusers = get_users('role=author');
foreach ($blogusers as $user) {
echo 'Author ID: '.$user->ID;
$author_id = $user->ID;
$ofertas = array();
// Second Loop
$i = 0;
$args = array(
'author' => $author_id,
'post_type' => 'ofertas'
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); // check if it has offers, if it has, loop over the offers
$ofertas[$i]['title'] = get_the_title(); // or anything you need
$i++;
endwhile; // close the
else: // if it has no post
continue; // we don't display the post
endif;
wp_reset_postdata();
} // end foreach
?>
Does this suit your requirements ?
Related
I am trying to collect a series of numbers inside a repeatable subfield in a custom post type, and then output the sum in a line of text. I've written the code below, but I have been having very little success in getting it to work.
Would appreciate if someone could have a look and amend for my education. Thanks
<?php
$args = array( 'post_type' => 'distribution-post' );
$amount = new WP_Query( $args );
$total = 0;
while ( have_rows('arr_dist') ) : the_row();
$total += intval( get_sub_field('amount'));
endwhile;
echo $total;
wp_reset_query();
?>
Try the below code. you have missed using have_post before using have_row.
$args = array( 'post_type' => 'distribution-post' );
$amount = new WP_Query( $args );
$total = 0;
if ( $amount->have_posts() ) {
while ( $amount->have_posts() ) : $amount->the_post();
while ( have_rows('arr_dist') ) : the_row();
$total += intval( get_sub_field('amount'));
endwhile;
endwhile;
wp_reset_postdata();
}
echo $total;
wp_reset_query();
<?php
$total = 0;
while(the_repeater_field('repeaterfield name' )):
$total += get_sub_field('repeatersubfield name' );
endwhile;
echo $total;
?>
change repeater field and sub-field as per your need.
In the wordpress front-page.php theme file i loop through all the woocoommerce products and show the featured-image.
<ul class="products">
<?php
// Setup your custom query
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail( ); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</ul>
Now i have some products which are variable products (different colors). Every color variation has its own image. How can i show all the different variation-images inside this loop? My plan is to create an image-slider with these images.
<?php
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_s = wc_get_product( $loop->post->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
// You may get all images from $variations variable using loop
echo '</pre>';
}
endwhile; wp_reset_query(); // Remember to reset ?>
I do not have tested yet. But hopefully it will work.
Use below code to get image urls
For Wc3+
foreach ( $variations as $variation ) {
echo $variation['image']['url'];
}
And for old Wc versions
foreach ( $variations as $variation ) {
echo $variation['image_src'];
}
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
);
$product_arrray = get_posts($args);
foreach($product_arrray as $prod)
{
$product_id = $prod->ID;
$product = wc_get_product($product_id);
$pvariation = $product->get_available_variations();
echo "<pre>";print_r($pvariation);echo "</pre>";
}
Id like to show the list of post thumbnails from specified user, using this code:
global $user;
$numposts = get_posts( array('author' => $user->ID) );
<?php foreach ($numposts as $numpost) {
the_post_thumbnail();
} ?>
Thumbnails not showing up though. Am I doing something wrong? This code is inserted in plugin php file. thx
Reworked and works:
$numposts = get_posts( array('author' => $user->ID) );
<?php
$my_query = new WP_Query( array( 'author' => $user->ID ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
{
echo the_post_thumbnail();
}
endwhile;
?>
although not sure why it didnt work first time.
I'm using already designed theme for wordpress, and now instead of regular blog posts I would like to display WooCommerce products (which are custom post types I persume).
This is the current query with display loop:
<?php
$args = array(
//'posts_per_page' => '2',
'paged' => get_query_var('paged')
);
$homepage_query = new WP_Query($args);
?>
<?php //query_posts('posts_per_page=4&paged='.get_query_var('paged')); ?>
<?php if ( have_posts() ) : ?>
<?php while ( $homepage_query->have_posts() ) : $homepage_query->the_post(); ?>
<?php if($style == 'blog_style') { ?>
<div id="blog-style" class="post-box">
<?php get_template_part('content', 'blog'); ?>
</div>
<?php } else { ?>
<div class="post-box grid_4 <?php aero_post_box_class(); ?>">
<?php get_template_part('content', ''); ?>
</div>
<?php } ?>
<?php endwhile; ?>
Is there a way to add options to $args so the loop displays WooCommerce products? I'm also using pagination with this loop, which is required on this project, so that's why it's important to use this loop.
You should be able to access products through the loop, setting the post_type arg to product:
<?php
// Setup your custom query
$args = array( 'post_type' => 'product', ... );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
This is the proper way to re-create and customize the WooCommerce product loop:
if(!function_exists('wc_get_products')) {
return;
}
$paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; // if your custom loop is on a static front page then check for the query var 'page' instead of 'paged', see https://developer.wordpress.org/reference/classes/wp_query/#pagination-parameters
$ordering = WC()->query->get_catalog_ordering_args();
$ordering['orderby'] = array_shift(explode(' ', $ordering['orderby']));
$ordering['orderby'] = stristr($ordering['orderby'], 'price') ? 'meta_value_num' : $ordering['orderby'];
$products_per_page = apply_filters('loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page());
$products_ids = wc_get_products(array(
'status' => 'publish',
'limit' => $products_per_page,
'page' => $paged,
'paginate' => true,
'return' => 'ids',
'orderby' => $ordering['orderby'],
'order' => $ordering['order'],
));
wc_set_loop_prop('current_page', $paged);
wc_set_loop_prop('is_paginated', wc_string_to_bool(true));
wc_set_loop_prop('page_template', get_page_template_slug());
wc_set_loop_prop('per_page', $products_per_page);
wc_set_loop_prop('total', $products_ids->total);
wc_set_loop_prop('total_pages', $products_ids->max_num_pages);
if($products_ids) {
do_action('woocommerce_before_shop_loop');
woocommerce_product_loop_start();
foreach($products_ids->products as $featured_product) {
$post_object = get_post($featured_product);
setup_postdata($GLOBALS['post'] =& $post_object);
wc_get_template_part('content', 'product');
}
wp_reset_postdata();
woocommerce_product_loop_end();
do_action('woocommerce_after_shop_loop');
} else {
do_action('woocommerce_no_products_found');
}
Using the code above, you would customize the wc_get_products() arguments to get the IDs of the products you want (if you have specific criteria). Once that code is in place, all the features of a native WooCommerce loop will be available to you—pagination, ordering, etc. This method is superior to WP_Query and get_posts() because those two methods can break.
I've written a more detailed blog post about custom WooCommerce loops here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
You can Also get Category using thi code
$terms = get_terms('product_cat');
foreach ($terms as $term) {
$term_link = get_term_link( $term, 'product_cat' );
echo '<li>' . $term->name . '</li>';
}
If You want only parent category then
wp_list_categories('taxonomy=product_cat&orderby=order&title_li=&depth=1');
I have setup a custom post type in Wordpress. It is a section where the client can upload documents in PDF format. The documents are divided into two categories - 'Downloadable Forms' and 'Menu' ; the Custom Field name for the category is 'document_category'
I am trying to run a query and only display the 'Downloadable Forms' on a page. Here is the code I normally use --- I'm hoping someone can help me add what I need to make it work?
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Thanks.
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) :
$the_query->the_post();
// Get all Advanced custom fields
$my_custom_fields = get_fields(get_the_ID());
// Does document_category field exists and is it equal with 'Downloadable Forms'?
if(isset($my_custom_fields['document_category']) && $my_custom_fields['document_category'] == 'Downloadable Forms'):
?>
<li><?php the_title(); ?></li>
<?php
endif;
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I've used the get_fields($post_id = false) function of the Advanced Custom Fields plugin that returns an array of all the posts' custom fields and then filtered it to match your needs.
Hope i've helped
Well, you can use the is_category(); inside loop and only display those posts, like so (using your same code):
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
if(is_category('Sownloadable Forms')){ // here the condition
?>
<li><?php the_title(); ?></li>
<?php
$i++;
} // here ends the IF statment
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Better yet: http://codex.wordpress.org/Function_Reference/is_category
Hope that helps.
Why dont you make it simpler and set a query based on custom field data?
<?php
$count = 1;
$args = array(
'post_type' => 'any',
'posts_per_page' => 4,
'meta_key' => 'display',
'meta_value' => 'true'
);
$query = new WP_Query();$query->query($args);
while ($query->have_posts()) : $query->the_post();
$do_not_duplicate[] = $post->ID;
?>
<!-- query content -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( 'Permalink to %s', 'advanced' ), the_title_attribute( 'echo=0' ) ); ?>" ></h2>
<?php the_excerpt() ;?>
<!-- query content -->
<?php $count++; endwhile; wp_reset_query(); ?>
That way any post with custom field key display and custom field value true will be shown on your query.