The $event_count variable doesn't prints anything, what am i doing wrong?
global $post;
$event_count = get_post_meta($post->ID, 'eab_event-bp-group_event', true);
$args = array(
'post_type' => 'incsub_event',
'posts_per_page' => -1,
'meta_key' => 'eab_event-bp-group_event',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'eab_event-bp-group_event',
'value' => $event_count,
),
),
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
echo '<ul>';
while ( $my_query->have_posts() ) {
$my_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
}
wp_reset_postdata();`
Related
How can I add an else statement to this code so if there are no posts, the output would be "There are currently no posts."
function jobs_posts_shortcode($atts, $content = null) {
global $post;
extract(shortcode_atts(array(
'category_name' => 'jobs',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts));
$args = array(
'category_name' => $category_name,
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
);
$output = '';
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
$output .= ''.get_the_title().'<br>'.get_the_date().'<br><br>';
}
wp_reset_postdata();
return ''. $output .'';
}
add_shortcode('jobs_posts', 'jobs_posts_shortcode');
You can simply check $posts are empty or not by using the PHP empty() function. check below code.
function jobs_posts_shortcode($atts, $content = null) {
global $post;
extract( shortcode_atts( array(
'category_name' => 'jobs',
'num' => '5',
'order' => 'DESC',
'orderby' => 'post_date',
), $atts ) );
$args = array(
'category_name' => $category_name,
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
);
$output = '';
$posts = get_posts($args);
if( !empty( $posts ) ){
foreach( $posts as $post ) {
setup_postdata($post);
$output .= ''.get_the_title().'<br>'.get_the_date().'<br><br>';
}
}else{
$output .= __('There are currently no posts.','textdomain');
}
wp_reset_postdata();
return ''. $output .'';
}
add_shortcode( 'jobs_posts', 'jobs_posts_shortcode' );
I have a post type called "rationale" and my taxonomy name is "company_list". In taxonomy there are list of company. Each company have many rationale.
I want to get latest rationale for each company. How can i do this ?
I try below code but it show all company list but data is duplicate
<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
'post_type' => 'rationale',
'paged'=>$paged,
'posts_per_page' => -1,
);
$loop = new WP_Query($myquery);
if( $loop->have_posts() ):
while( $loop->have_posts() ):
$loop->the_post(); global $post; ?>
<?php $terms = get_the_terms( $post->ID, 'company_list' );
foreach($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}?>
<?php endwhile; ?>
<?php endif; ?>
You need to get latest term and use tax_query
$args_query = array(
'post_type' => array('rationale'),
'paged' => $paged,
'posts_per_page' => -1,
);
$terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
));
if (!empty($terms) && is_array($terms)) {
$args_query['tax_query'] => array(
array(
'taxonomy' => 'company_list',
'field' => 'term_id',
'terms' => array($terms['0']->term_id), // single or array with id's
),
),
}
$query = new WP_Query($args_query);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_the_terms($post->ID, 'company_list');
foreach ($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}
}
} else {
// no post found
}
wp_reset_postdata();
try this
$terms = get_terms( array(
'taxonomy' => 'your taxonomy name',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
echo "<pre>";
print_r($terms_row);
echo "</pre>";
}
Thanks
I am trying to display posts of specific category(taxonomy) that is 'Book1'.
I tried to display it with the following code.
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'id',
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
In output no any item is displayed.
$custom_terms = get_terms('Book1');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
try this code
$args = array(
'post_type' => 'book',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'Book1',
'field' => ''term_id', // here you are worng name too
'terms' => 1
)
)
);
echo '<br>';
$postss = get_posts( $args );
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->post_title. '<br>';
$i++;
}
}
?>
// best solution
<?php
$query = new WP_Query( array(
'post_type' => 'book', // name of post type.
'tax_query' => array(
array(
'taxonomy' => 'Book1', // taxonomy name
'field' => 'term_id', // term_id, slug or name
'terms' => 1, // term id, term slug or term name
)
)
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();
?>
I did it by doing like this. Thanks all.
$args = array(
'post_type' => 'book',
'cat' => '35'
);
echo '<br>';
$postss = query_posts($args);
if ( ! empty( $postss ) && is_array( $postss ) ) {
// Run a loop and print them all
?><?php $i=1;
foreach ( $postss as $termm ) { ?>
<?php echo ' '.$i.' '.$termm->post_title. '<br>';$i++;?>
<?php
}
}
I have create a loop with a custom query. This query contains a php parameters that i m getting from url with GET method. This the code at my loop page:
$cat = get_queried_object();
echo '<h1 class="childcatdes">'. $cat->name . '</h1>';
echo '<p class="childcatdescr">'. $cat->description . '<br><br></p>';
do_action( 'woocommerce_before_single_product' );
$posts_per_page = 12;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'product',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'page' => $paged,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => $cat->name,
),
array(
'taxonomy' => 'manufacturers',
'field' => 'slug',
'terms' => $_GET['filter_manufacturers'],
'operator' => 'IN'
)
),
'orderby' => 'name',
'order' => 'ASC',
);
$custom_query = new WP_Query( $product_args );
if($custom_query->have_posts()) {
echo '<ul';
while ($custom_query->have_posts() ) : $custom_query->the_post();
echo '<li>';
$link = get_the_permalink();
echo '' . get_the_title() . '';
echo '</li>';
endwhile;
echo '</ul>';
}
else {
echo 'No post found.';
}
?>
<nav class="pagination">
<?php pagination_bar( $custom_query); ?>
</nav>
And i use this code to my functions.php file for the function pagination_bar
function pagination_bar( $custom_query) {
$total_pages = $custom_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
'current' => $current_page,
'format' => 'page/%#%/',
'total' => $custom_query->max_num_pages,
'add_args' => array(
'filter_manufacturers' => $_GET['filter_manufacturers'],
)
));
}
}
The problem is that the pagination (even if it counts the posts correct at first page) is not working correct. I 'm getting 404 error at second page.
I've created a custom shortcode to display products with a minimum stock amount and would like to add pagination to the results as well as calling the woocommerce-ordering dropdown to be displayed on the page.
Here's the shortcode:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
Any help would be very much appreciated!
Kind regards,
JP
Okay, so I've got pagination working and tidied things up a bit (it was throwing some errors in the debug log), the code now looks like this:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $product, $woocommerce, $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'title',
'order' => 'asc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
),
$atts, 'minimum_stock'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
if($products->max_num_pages>1){
?>
<nav class="woocommerce-pagination">
<?php echo paginate_links( apply_filters(
'woocommerce_pagination_args', array(
'base' => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
)
)
);
?>
</nav>
<?php }
woocommerce_reset_loop();
wp_reset_postdata();
$return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
Does anyone know how I can now call the woocommerce-ordering dropdown?
I've tried adding:
<?php do_action( 'woocommerce_before_shop_loop' ); ?>
But this doesn't seem to work, when I check the page it does have the 'woocommerce-notices-wrapper' but there's no sign of the 'woocommerce_catalog_ordering' that I thought should also be called with 'woocommerce_before_shop_loop'.
Any and all help will be very much appreciated :)
Update:
I found this question Adding 'sort by' drop down on custom page using woocommerce short code which gave me the answer I needed to add the woocommerce-ordering dropdown.
Here's my updated code:
// Minimum Stock Shortcode
add_shortcode( 'minimum_stock', 'minimum_stock_shortcode' );
function minimum_stock_shortcode( $atts ) {
global $woocommerce_loop;
// Attributes
$atts = shortcode_atts(
array(
'limit' => '40',
'columns' => '5',
'orderby' => 'date',
'order' => 'desc',
'category' => '',
'cat_operator' => 'IN',
'stock' => '',
), $atts );
if ( ! $atts['category'] ) {
return '';
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'],
$atts['order'] );
$orderby = 'date';
$order = 'desc';
if ( isset( $_GET['orderby'] ) ) {
$getorderby = $_GET['orderby'];
}
if ($getorderby == 'date') {
$orderby = 'date';
$order = 'desc';
} elseif ($getorderby == 'sku_desc') {
$orderby = 'meta_value';
$order = 'desc';
$meta_key = '_sku';
} elseif ($getorderby == 'sku_asc') {
$orderby = 'meta_value';
$order = 'asc';
$meta_key = '_sku';
}
$args = array(
'post_type' => array( 'product', 'product_variation' ),
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['limit'],
'orderby' => $orderby, // $ordering_args['orderby'],
'order' => $order, // $ordering_args['order'],
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_stock',
'value' => $atts['stock'],
'compare' => '>='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $atts['columns'];
if ( $products->have_posts() ) : ?>
<div style="width:100%;">
<div style="float:right">
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters( 'woocommerce_catalog_orderby', array(
'date' => __( 'Sort by latest', 'woocommerce' ),
'sku_asc' => __( 'A-Z / Low to High Numbers', 'woocommerce' ),
'sku_desc' => __( 'Z-A / High to Low Numbers', 'woocommerce' )
) );
foreach ( $catalog_orderby as $id => $name )
echo '<option value="' . esc_attr( $id ) . '" ' . selected( $getorderby, $id, false ) . '>' . esc_attr( $name ) . '</option>';
?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key )
continue;
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
</div>
</div>
<div style="clear:both;"></div>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
if($products->max_num_pages>1){
?>
<nav class="woocommerce-pagination">
<?php echo paginate_links( apply_filters(
'woocommerce_pagination_args', array(
'base' => esc_url( str_replace( 999999999, '%#%', remove_query_arg( 'add-to-cart', get_pagenum_link( 999999999, false ) ) ) ),
'format' => '',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $products->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
)
)
);
?>
</nav>
<?php }
woocommerce_reset_loop();
wp_reset_postdata();
$return = '<div class="woocommerce columns-' . $atts['columns'] . '">' . ob_get_clean() . '</div>';
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
I hope someone might find this helpful :)
Kind regards,
JP