How do you include custom field in Shortcode output? - wordpress

with help from another developer, I have created the following for a custom shortcode:
add_shortcode( 'children' , 'display_custom_post_type_v1' );
function display_custom_post_type_v1($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'operator' => 'AND',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}
So far this works really well for my application, but there is one additional item I would like to include. These custom post types include meta boxes (custom fields) and I would like to include this in the output. Typically I use something like:
<?php echo get_post_meta( $post->ID, '_cd_birthdate', true ); ?>
But I can't figure out how to include it in the output of the shortcode. I've tried this line, but only the div area shows up, but no content:
<div>' . get_post_meta( $post->ID, '_cd_birthdate', true ) . '</div>
Any suggestions would be greatly appreciated.

I think '$post->ID,' only works when in the WP loop. Try 'get_the_ID()' instead.
Like...
<div>' . get_post_meta( get_the_ID(), '_cd_birthdate', true ) . '</div>
See this

Related

How do I filter my custom post types by taxonomy with checkboxes?

I am trying to filter my posts from a custom post type called 'courses'. I've created a custom taxonomy called 'course_types' which has 2 terms; 'Bundled Courses' and 'Single Courses'. I have a form with checkboxes, with which I would like to filter the custom posts, and have used a variety of combinations of tax_query operators ("AND, OR, NOT IN, etc"), but I am unabke to acheieve the expected results. Any help on this would be greatly appreciated. Thanks guys.
The Query
<?php
$args =
array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_types',
'field' => 'term_id',
'terms' => $_GET['course_types'],
),
'relation' => 'AND',
),
)
?>
Form with checkboxes
<form method="GET">
<?php $terms = get_terms( array(
'taxonomy' => 'course_types',
'hide_empty' => false,
)
);
foreach($terms as $term) {
echo '<label><input type="checkbox" name="course_types[]" value="' . $term->name
.'">' . $term->name . '</label>';
} ?>
<input type="submit" value="Filter">
</form>
<?php
The Results
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', 'courses' );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Turns out that the issue was that form was returning a name ('Single Courses' and 'Bundled Courses'), bu the query was expecting a term_id.
Corrected code
<form method="GET">
<?php $terms = get_terms( array(
'taxonomy' => 'course_types',
'hide_empty' => false,
) );
foreach($terms as $term) {
echo '<label><input type="checkbox" name="course_types[]" value="'
. $term->term_id .'">' . $term->name . '</label>';
} ?>
<input type="submit" value="Filter">
</form>
The query must contain a conditional if not filtered. I used a ternary.
<?php
$terms = get_terms( 'course_types' );
$term_ids = wp_list_pluck( $terms, 'term_id' );
$args =
array(
'post_type' => 'courses',
'tax_query' => array(
array(
'taxonomy' => 'course_types',
'field' => 'term_id',
'terms' => $_GET ? $_GET['course_types'] : $term_ids,
),
'relation' => 'AND',
),
);
?>

Group WordPress posts by category

after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);
thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>
Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help

Ajax filter for wordpress

after a lot of research i came across this: https://rudrastyh.com/wordpress/ajax-post-filters.html
But I can only get it to work when there are two options selected.
I'll give you some context:
I have a CPT named 'Contratistas' that has two custom taxonomies 'especialidad' and 'industria', they both have two terms each 'especialidad' -> 'tecnologia' and 'auditoria'; 'industria' -> 'cultivo' and 'depocito'
here is my function:
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
// for taxonomies / categories
if( isset( $_POST['filtroEspecialidad'] ) && isset ($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
//'relation' => 'AND',
array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
),
array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
),
);
} elseif( !isset($_POST['filtroEspecialidad'] ) && isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'field' => 'id',
'terms' => $_POST['filtroIndustria']
);
} elseif( isset( $_POST['filtroEspecialidad'] ) && !isset($_POST['filtroIndustria']) ) {
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'field' => 'id',
'terms' => $_POST['filtroEspecialidad']
);
}
It works if you select something from both taxonomies, but when one is empty it says 'there are no post'
as a bonus I will like to show all the post before filtering.
I hope somebody can help me! Thanks! I'm fairly new to Wordpress
EDIT! Here is my js and my form, i'm at a loss here I can't figure out what's wrong :(
jQuery(function($){
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('.filtrar').text('Procesando...'); // changing the button label
},
success:function(data){
filter.find('.filtrar').text('Filtrar'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
my php file:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<div class="titulo mb-3">
<h3>Especialidad</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'especialidad', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroEspecialidad"><option value="">Seleccione una especialidad...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<div class="titulo my-3">
<h3>Industrias</h3>
</div>
<?php
if( $terms = get_terms( array( 'taxonomy' => 'industria', 'orderby' => 'name' ) ) ) :
echo '<select name="filtroIndustria"><option value="">Seleccione una industria...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button class="my-3 filtrar">Filtrar</button>
Clear
<input type="hidden" name="action" value="myfilter">
</form>
You may want to give the Taxonomy Parameters of WP_Query() another glance. Unfortunately, WordPress is a little loose with it's id parameter names contextually. I'm not entirely sure your initial "both" is working like you intend either, because 'field' => 'id' is actually invalid.
From the docs:
field (string) – Select taxonomy term by. Possible values are term_id, name, slug or term_taxonomy_id. Default value is term_id.
id isn't actually a valid option. If you're just using the term_id, you should be able to omit that. You can also just programatically add the tax_query array arguments based on if that filter is set, instead of checking for "both set, this set/that unset, this unset, that set", something like this perhaps?
function misha_filter_function(){
$args = array(
'orderby' => 'date', // we will sort posts by date
'order' => $_POST['date'], // ASC or DESC
'post_per_page' => -1,
'post_type' => 'contratista'
);
if( isset($_POST['filtroEspecialidad']) || isset($_POST['filtroIndustria']) ){
$args['tax_query'] = array();
if( isset($_POST['filtroEspecialidad']) ){
$args['tax_query'][] = array(
'taxonomy' => 'especialidad',
'terms' => $_POST['filtroEspecialidad']
);
}
if( isset($_POST['filtroIndustria']) ){
$args['tax_query'][] = array(
'taxonomy' => 'industria',
'terms' => $_POST['filtroIndustria']
);
}
if( count($args['tax_query']) > 1 ){
$args['tax_query']['relation'] = 'AND';
}
}
// Run WP_Query with new $args, etc.
}
I'm not sure if the $_POST values are arrays or single numbers, but you may want to validate those with array_map and/or absint if you're using user-supplied input, but if they're just IDs, the above should work for now.

Custom post type Wordpress query by category

I have the following query which outputs a list of categories for my custom post type called STORIES.
<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div> ';
}
?>
This outputs a list of links for my categories and works great.
My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.
So my query lists the categories...
- Apples
- Oranges
- Bananas
If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?
Any ideas? Can't get any solution to work.
I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?
<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'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 '<p>'.get_the_title().'</p>';
endwhile;
}
}
?>
you can create custom taxonomy template for custom post : LINK
Hope this help:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$query = new WP_Query( $args );
Class Reference/WP Query

Can't get WP_Query custom post type's category posts properly

I have some problem with WP_Query for custom post types, I can't get custom posts by categories properly. I have:
function my_render_posts_block( $attributes ) {
$tax_query = array(
array(
'taxonomy' => 'my_cpt_category',
'field' => 'term_id',
'terms' => $attributes['postCategories']
)
);
$args = array(
'post_type' => 'my_cpt',
);
if($attributes['postCategories']) {
$args['tax_query'] = $tax_query;
}
$query = new WP_Query($args);
$posts = '';
if($query->have_posts()) {
$posts .= '<ul>';
while ($query->have_posts()) {
$query->the_post();
$posts .= '<li>' . get_the_title() . '</li>';
}
$posts .= '</ul>';
wp_reset_postdata();
return $posts;
} else {
return '<div>' . __("No Posts Found", "my-blocks") . '</div>';
}}
Actually it's working for one term, but if I choose 2 terms, it will show posts only for first term.
In $attributes['postCategories'] I pass terms ids, if var_dump it I get string(7) "209,208", where 209 and 208 are correct ids of terms.
What am I doing wrong?
Thanks.
You passed as a string instead of an array
$termsArray = explode(',', $attributes['postCategories']);
$tax_query = array(
array(
'taxonomy' => 'my_cpt_category',
'field' => 'term_id',
'terms' => $termsArray
)
);

Resources