Custom taxonomy looping - wordpress

I have a custom taxonomy called Specialties for Custom post type Candidates.
And entered items in Specialties A, B
I have created shortcode with attributes for looping A and B separately
like [candidates show="a"] for A candidates
and [candidates show="b"] for B candidates
I need to create a loop condition if any cpt item has both A & B Selected
then It will show that item in both shortcodes.
Here is the code
function show_all_custom_post_shortcode($attr) {
ob_start();
$a = shortcode_atts( array(
'show' => 'a',
), $attr );
$args = array(
'post_type' => 'candidates',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'candidate_speciality',
'field' => 'slug',
'terms' => $a['show'],
),
)
);
$q = new WP_Query( $args );
while ( $q->have_posts() ) : $q->the_post();
$imgurl = wp_get_attachment_url( get_post_thumbnail_id($q->ID) );
include('candidates/candidates-loop.php');
endwhile;
wp_reset_query();
return ob_get_clean();
}
add_shortcode('all-cpt', 'show_all_custom_post_shortcode'); // [all-cpt show='a'], [all-cpt show='b']

Related

WordPress: How can get all product of brand from specific a category with WooCommerce Plugin?

I am trying to print all products of a brand on from category page.
I find the wp_query but I can't print the current brand name of the brand to show all products in this page.
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
For example, i need to get all product of brand name (XXX) that fount in category name (YYY)
This is answer after search :
$args = array(
'post_type' => array('post','product'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 82,
'field' => 'id'
),
array(
'taxonomy' => 'brand',
'terms' => 81,
'field' => 'id'
),
)
);
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
}
}

Show user post count while using "ultimate member" and "co-author"

I'm using the plugin ultimate member and co-author
and in the author page in the post tab
i'm trying to show the numbers of posts the user has written with this line
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
endwhile;
endif;
Would appreciate your help for the correct code
You will need a counter.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
$count=0;
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
$count++;
endwhile;
endif;
echo $count;

Dynamic call of shortcode with parameters

I have this function:
function test_q($atts){
$args = shortcode_atts(array(
'post_type' => 'product',
'columns' => 4,
'posts_per_page' => 12,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'mugs' ),
),
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => array( 'football' ),
),
),
), $atts);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
woocommerce_product_loop_start();
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
woocommerce_product_loop_end();
} else {
echo __( 'No products found' );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode('testasd', 'test_q');
I want to pass dynamic parameters to 'mugs' and 'football' with the shortcode. I am filtering products by category and tag. This function is working fine but I want to pass those two parameters through shortcode to make it dynamic.
I need to call this shortcode each time with different 'mugs' and 'football' term. For example 't-shirts' and 'basketball'. How will i do that?
You can do it easily via extracting the shortcode and passing the values via $atts from the shortcode.
After your arguments , you can pass it like the following ,
if($args['tax_query']['taxonomy'] == 'product_cat'){
$args['tax_query']['terms'] = $atts['terms_cat'];
}
if($args['tax_query']['taxonomy'] == 'product_tag'){
$args['tax_query']['terms'] = $atts['terms_tag'];
}
Then add 2 parameters in the shortcode,
[testasd terms_cat='t-shirts' terms_tag='basketball']

Advanced Custom Fields: querying posts filtered by custom field values

I’m trying to query my CPT posts based on sub custom field values as in example 5 on
this tutorial.
My CPT (named “trip”) has a repeater field called “departure_date”, which has a sub field called “departure_day”:
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'departure_date'", "meta_key LIKE 'departure_date'", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$args = array(
'numberposts' => -1,
'post_type' => 'trip',
'meta_query' => array(
array(
'key' => 'departure_day',
'value' => 0,
'compare' => '>'
)
)
);
$get_trips_date = new WP_Query( $args );
if( $get_trips_date->have_posts() ):
while ( $get_trips_date->have_posts() ) : $get_trips_date->the_post();
if( get_field('departure_date') ) {
while( has_sub_field('departure_day') ) {
echo get_sub_field('departure_day');
}
}
endwhile;
endif;
wp_reset_query();
Although sub field “departure_day” is populated for all posts, this code returns nothing. Why?
Your $args should be like this
`
$args = array(
'numberposts' => -1,
'post_type' => 'trip',
'meta_query' => array(
array(
'key' => 'departure_date_%_departure_day',
'value' => 0,
'compare' => '>'
)
)
);
`

wordpress advanced custom fields order posts by date-picker

For those familiar with the ACF plugin...
I have some events posts that are currently displaying in post order (see code below). I would like them to display in the order specified by the date-picker.
Can anyone tell me what to amend in the following - I have tried the documentation on the site, but my PHP is basic.
It says I need to add
'orderby' => 'meta_value_num',
But no joy.
<?php function le_whatson_aside() {
//THis loop is for the CPT
$args = array(
'post_type' => 'events', // enter your custom post type
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page'=> '10', // overrides posts per page in theme settings
'tax_query' => array(
array(
'taxonomy' => 'audience', //name of custom taxonomy
'field' => 'slug',
'terms' => 'everyone' //name of category
)
)
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
?>
<div>
<h2>What's On</h2>
</div>
<div class="whatson entry-content">
<?php
while( $loop->have_posts() ): $loop->the_post(); global $post;
?>
<p class="whatson-date"><?php echo date("dS F Y",strtotime(get_field('date')));?></p>
<h4 class="whatson-title"><?php echo get_the_title(); ?></h4>
<?php
endwhile;
?>
</div>
<?php
endif; }
Thanks all.
Try
orderby=date or `post_date`
If not the easiest way is to save your custom field 'startdate' as a unix timestamp. To do this, add the following to your theme's functions.php
// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'startdate', true);
if($startdate) {
$dateparts = explode('/', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
The do:
$today = time();
$args = array(
'post_type' => 'events',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$events = $query->posts;
Got it from here
I spent hours looking for this and I can confirm it works. See my code below.
If you're trying for a loop on a page with other loops, with a bunch of template parts in there, and you would also like to sort by a category, it goes :
$today = time();
$the_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'start',
'value' => $today,
'compare' => '>=',
)
),
'tax_query' => array(
array (
'taxonomy' => 'the_taxonomy',
'field' => 'slug',
'terms' => 'the-name'
)
),
'meta_key' => 'start',
'orderby' => 'meta_value',
'order' => 'ASC',
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'content-events' );
endwhile;
wp_reset_postdata();
Of course, you have to include Unix the function beforehand.
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'start', true);
if($startdate) {
$dateparts = explode('_', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
I would approach it like this. Create a "named" meta query and then order by that query. The meta query uses "EXITS" to filter out posts that don't have a date set. This works with the ACF date picker when the dates are saved to the database using the default format d/m/Y. This approach also works with the datetime picker.
$query = new WP_Query([
"meta_query" => [
"custom_date" => [
"key" => "date",
"compare" => "EXISTS",
"type" => "DATETIME",
]
],
"orderby" => [
"custom_date" => "ASC",
],
]);
Be sure to update the value for key to whatever your ACF field name is.

Resources