I'm working on this plugin for wordpress and i am stuck on a query that won't be reset.
In the following function:
function WPSM_artists_autocomplete(){
$response = array();
query_posts('post_type=artist&posts_per_page=-1');
if (have_posts()) : while (have_posts()) : the_post();
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'artist-icon');
$image_url = $image_url[0];
$response[] = array( get_the_ID() , get_the_title() , null, '<img src="'.$image_url.'" />'. get_the_title());
endwhile; endif;
wp_reset_query();
// Write JSON file
$output = json_encode($response);
$data = WPSM_CACHE_DIR."/data.json";
$fh = fopen($data, 'w') or die("can't open file");
fwrite($fh, $output);
fclose($fh);
// Return JSON url
echo WPSM_CACHE_URL."/data.json";
}
I use a query_posts to populate a metabox. But the wp_reset_query(); doesn't seem to work properly. This affects all other metaboxes and post related option. The global $post variable is set to the latest value of this query, and not the default value of the posts edit page.
I'd love to hear how to solve this plugin. Could use everything to get me in the right direction. Thanks in advance!
Cheers,
Ronny
I came across this today and found a fix.
You will need to store the original $post before starting a new loop and then at the end of your function you will need to set it back.
Before you function assign $post to a temporary variable.
$original_query = $wp_query;
Then at the end of your function set it back.
$wp_query = $original_query;
wp_reset_postdata();
Not sure if the above works in your case as I was using a custom query.
I have posted my code below so you can have a look.
global $wpdb;
global $post;
$originalpost = $post;
$querydetails = "
SELECT *
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'projects'
AND $wpdb->posts.post_status = 'publish'
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querydetails, OBJECT);
if ($pageposts) {
foreach ($pageposts as $post) {
setup_postdata($post);
$postID = get_the_ID();
echo '<option value="';
echo $postID . '"';
foreach ($meta as $m) {
if ($postID == $m) echo ' selected="selected" ';
}
echo '>';
echo the_title();
echo '</option>';
}
}
echo "</select>";
$this->show_field_end($field, $meta);
$post = $originalpost;
Related
I need help creating a wp shortcode that shows a list of users (by role) with a check if they have commented on a certain post. Post ID and user role are shortocode parameters.
In order to do this I thought of merging this handmade shortcode ( it shows for a specified user if whether or not commented on a post, [fancyshortcode user_id="" post_id=""] ):
function fancycheck( $attributes ) {
// Things that you want to do.
$args = array(
'user_id' => get_the_ID(),
'post_id' => get_the_ID()
);
if( isset( $attributes['post_id'] ) ){
$args['post_id'] = absint($attributes['post_id']);
}
if( isset( $attributes['user_id'] ) ){
$args['user_id'] = absint($attributes['user_id']);
}
$comment = get_comments($args);
if(!empty($comment)){
return '<div class="ccount">YES</div>';
}
else {
return '<div class="ccountniet">NO</div>';
}
return "";
}
// register shortcode
add_shortcode('fancyshortcode', 'fancycheck');
with a shortcode to show users list... something like this:
function wcr_list_users($atts) {
// I don't use shortcode_atts because I want to have a dynamic number of attributes
$query = new WP_User_Query($atts);
$results = $query->get_results();
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?></li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('list_users', 'wcr_list_users');
but it is a too complicated task for my skills. So I tried to simplify the shortcode just indicating for each user the number of comments left for a particular post.. something like this:
function listone($atts) {
$query = new WP_User_Query($atts); $results = $query->get_results();
$args = array( 'post_id' => get_the_ID() );
$comments = get_comments('post_id');
// no results
if (empty($results)) {
return;
}
// when we have results
ob_start();
echo '<ul>';
foreach ($results as $item) {
?>
<li>
<?php echo esc_html($item->display_name); ?><br />
Comments: <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?>
</li>
<?php
}
echo '</ul>';
return ob_get_clean();
}
add_shortcode('listonesc', 'listone');
but obviously this <?php echo ($comment->comment_author .'--comment--'.$comment->comment_content);?> doesn't work.
Can anyone help me or give me directions on which way to go? Thank you
I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>
I'm trying to add some sidebar content to my WordPress search results page where the top categories and tags, related to the current search query, are displayed. I have been able to generate the content, but there are a massive amount of duplicates also displaying. Click for my example page.
I've tried two ways of doing this, both with no success. Both were pulled from previous threads with similar questions. I would prefer avoiding 3rd party plugins if possible. Any ideas would be appreciated. Thanks
Method #1:
function list_search_cats() {
// Start the Loop
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats = get_the_category();
if (! in_array($cats, $uniqueCats)) :
$uniqueCats[] = $cats;
foreach($cats as $cat) :
echo '<li><a class="tag" href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a></li>';
endforeach;
endif;
endwhile;
}
Method #2:
function list_search_cats() {
// Start the Loop
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats = get_the_category();
$cats = array_unique($cats, SORT_REGULAR);
foreach($cats as $cat) :
echo '<li><a class="tag" href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a></li>';
endforeach;
endwhile;
}
It looks to me like you're just missing the actual check of each category. You may need an extra foreach loop to check for dupes, then use that unique array in another foreach to display the categories. Try this:
function list_search_cats() {
// Array to put unique cats in
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats[] = get_the_category();
// first foreach to check each cat and put in to new array
foreach($cats as $cat) {
if(!in_array($cat, $uniqueCats)) {
$uniqueCats[] = $cat;
}
}
// second foreach to display the list
foreach($uniqueCats as $cat_li) {
$term = get_term_by('name', $cat_li, 'category');
echo '<li><a class="tag" href="'.get_category_link($term->term_id)).'">' . $cat_li . '</a></li>';
}
endwhile;
}
I have next code:
<?php
global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = get_the_terms($category->slug,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
?>
The code will display category description. The problem - on sub-category it will display the sub-category description + the parent description.
How i can display the description separate: in parent - the parent description, and on sub - only the sub description?
Try this and let me know if it helped you
add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description',
12);
function custom_add_product_description ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
$description= $prod_term->description;
echo '<div>'.$description.'</div>';
}
The answer:
<?php
global $post;
$terms = get_the_terms( 'product_cat',$post->ID);
echo '<div style="direction:rtl;">';
echo category_description( get_category_by_slug($terms)->term_id);
echo '</div>';
?>
You can use this code to display the product category description -
<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>
Anajanas answer works very well, It can also be modified to do what I needed
which was to display a ACF custom field of a woocoommerce product child category on the shop catgegory page.
This is my code to add in functions.php
12);
function xyz ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
echo "<div class='designercatname'>".get_field('designer_name', $prod_term )."</div>";
}```
Is it possible to loop in a wordpress plugin?
I've created this plugin that utilizes a wordpress loop to grab some info about posts in my custom post type of events:
function getEventsFeed() {
$args = array( 'post_type' => 'events' );
$loop = new WP_Query( $args );
$htmlOutput = '<h2>Events</h2>';
while ( $loop->have_posts() ) : $loop->the_post();
$date = get_post_meta($post->ID, 'events_0');
$location = get_post_meta($post->ID, 'events_9');
$htmlOutput .= '<tr><td>' . the_title() . '</td><td>' . $date[0] . '</td><td>' . $post->post_title .'</td><td>' . $location[0] . '</td></tr>';
endwhile;
$htmlOutput .= '</div>';
echo $htmlOutput;
}
Problem is only the the_title info is being returned. $post is not working within the loop so $post->ID and $post->post_title are not being returned. I'm using this exact code in another page template and it returns all the data correctly. I'm not sure why it won't return when I use it in a plugin.
Any ideas?
Try adding
global $post;
to the beginning of your function. $loop->the_post() will set the global $post variable, but it is not available inside your function scope.