get top posts link by add_query_arg() in wordpress - wordpress

in wordpress we have add_query_arg() function to generat a url query from public key and value .
i retrive the cat link from query with add_query_arg() function like this :
$retrive ='424'; //my custom cat id .
$postnumber = '5';
$my_query = new WP_Query(array('showposts' => $postnumber , 'cat' => $retrive));
$query_name = get_cat_name($retrive);
$key = 'cat';
$value = $retrive;
// generat query link
$query_link= esc_url( add_query_arg( $key , $value ,site_url('/') ) );
echo '<a class="btn btn-lg btn-blue" href="'.$query_link.'">'.$query_name.'</a>';
now ,i want the link of "top" and "rand" post with add_query_arg() function.
this is my query's:
$top_query = new WP_Query(array('meta_key'=>'post_views_count','orderby'=>'meta_value_num','showposts'=>$postnumber,'order'=>'DESC'));
$key = ?;
$value = ?;
$rand_query = new WP_Query(array('showposts' => $postnumber , 'orderby' => 'rand'));
$key = ?;
$value = ?;
(i need this links for my custom tab_widget plugin that user can see the link of other post of current tab.)
how can do this ?

Related

Get all values of a ACF field in all posts and the links to the posts

I have created an ACF field where I can add 1 keyword per post. I want to get now a list of all keywords set in all my posts and sort it by alphabet and add a link to the post where it was found. Every keyword will be unique. So it would be a kind of table of content. How can I do that programatically? I have no idea right now on how to loop through posts and get field values.
Place the following function in functions.php . Use it directly in your template or as shortcode or w/e
function keywords_post_list() {
//We build our query for posts containing any value in meta field
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'keyword', //change with your meta key
'value' => '',
'compare' => '!='
)
);
$query = new WP_Query($args);
global $post;
$items = array();
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
//Looping each post we collect the keyword and the link for a post.
//Grab any other information if you need and add in the array
$keyword = get_post_meta($post->ID,'keyword',true);
$link = get_the_permalink($post->ID);
//Our array
$items[] = array('keyword'=> $keyword,'link' => $link);
endwhile;
endif;
// We need to sort results by keyword currently ASC
array_multisort(array_column($items, 'keyword'), $items);
// If we need to sort DESC uncommnet bellow coment above
// array_multisort(array_column($items, 'keyword'),SORT_DESC, $items);
// error_log(print_r($items,true));
if($items):
echo '<ol class="keyword-list">';
foreach($items as $item):
$keyword = $item['keyword'];
$link = $item['link'];
echo '<li>'.$keyword.'</li>';
endforeach;
echo '</ol>';
endif;
}

How to call posts from this functions code?

I have a piece of code for displaying related posts by the post tag, but I don't know how to call posts in theme,
so I have this code which I have to put in my theme functions file:
function exe_get_related_posts_by_common_terms( $post_id, $number_posts = 0, $taxonomy = 'post_tag', $post_type = 'post' ) {
global $wpdb;
$post_id = (int) $post_id;
$number_posts = (int) $number_posts;
$limit = $number_posts > 0 ? ' LIMIT ' . $number_posts : '';
$related_posts_records = $wpdb->get_results(
$wpdb->prepare(
"SELECT tr.object_id, count( tr.term_taxonomy_id ) AS common_tax_count
FROM {$wpdb->term_relationships} AS tr
INNER JOIN {$wpdb->term_relationships} AS tr2 ON tr.term_taxonomy_id = tr2.term_taxonomy_id
INNER JOIN {$wpdb->term_taxonomy} as tt ON tt.term_taxonomy_id = tr2.term_taxonomy_id
INNER JOIN {$wpdb->posts} as p ON p.ID = tr.object_id
WHERE
tr2.object_id = %d
AND tt.taxonomy = %s
AND p.post_type = %s
GROUP BY tr.object_id
HAVING tr.object_id != %d
ORDER BY common_tax_count DESC" . $limit,
$post_id, $taxonomy, $post_type, $post_id
)
);
if ( count( $related_posts_records ) === 0 )
return false;
$related_posts = array();
foreach( $related_posts_records as $record )
$related_posts[] = array(
'post_id' => (int) $record->object_id,
'common_tax_count' => $record->common_tax_count
);
return $related_posts;
}
and now I want to call 10 posts from the above code in my single.php that can show post title and post thumbnail with the link to every post.
try something like that:
$query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => 'your-tag',
'order' => 'DESC',
'posts_per_page' => 10
));
Then you will get latest 10 posts
This function is very useful, people continually looking for ways to get related posts
What this function does:
The code you are using is a working function that is returning an array of posts related to current post depending on common terms.
How this function works:
You can call the function from your single.php file and as input you have to provide 4 info. The current post ID, the number of related posts to return, the post_term to filter the related posts (post_tag) and at last the post_type.
Example function call: exe_get_related_posts_by_common_terms(get_the_ID(), 10, 'post_tag', 'post')
The above example will return 10 post ids as array based on current post ID and the tag
- Returning the post array and loop through ID's
Now we only need a custom wp_query in order to loop through the array and format the output.
Example wp_query:
$args = array(
'post_type' => 'post',
'post__in' => $related_post
);
// The Query
$related_query = new WP_Query( $args );
Result:
Full working example returning the post titles in an unordered list:
<?php
$cpid = get_the_ID(); // get current post id
$related_posts = exe_get_related_posts_by_common_terms($cpid, 10, 'post_tag', 'post');
$posts_array = array_column($related_posts, 'post_id'); // new single dimension array with the ID's
$args = array(
'post_type' => 'post',
'post__in' => $posts_array
);
// The Query
$related_query = new WP_Query( $args );
if ($related_query->have_posts()) : ?>
<ul>
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
You can now set the look and fill as you like

Adding a list of emails to wp_mail() in WordPress

I am trying to add a list of email addresses to wp_mail() from a certain user role. I have a comma delimited list stored as $user_email_list but cannot get that to output into the $multiple_recipients array.
Any help would be greatly appreciated.
// Get users and their roles, create list of emails to send notification to.
$user_args = array(
'role__in' => 'test_role',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($user_args);
foreach ( $users as $user ) :
$user_email_list = $user->user_email . ', ';
endforeach;
// Email Data
$multiple_recipients = array(
$user_email_list
);
$subject = $post->post_title;
$body = $post->post_content;
Update your code of foreach and check your $multiple_recipients variable at final, it would be comma separated value.
foreach ( $users as $user ) :
$user_email_list[] = $user->user_email;
endforeach;
$multiple_recipients = implode(', ', $user_email_list);

Contact Form 7 and Custom post type

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type "trade Show Material" - The post type contains the fields "name" "number" "description" "photo" . The idea will be that each piece can be selected from the form . Can anyone offer the general direction for this? Should I perhaps be using another plugin entirely?
Maybe you can use the wpcf7_form_tag filter hook for this.
If you want to use a custom post type as the options of a dropdown (select) you can add something like the example below in your functions.php:
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'your-field-name' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'your-custom-post-type',
'orderby' => 'title',
'order' => 'ASC',
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$tag['raw_values'][] = $custom_post->post_title;
$tag['values'][] = $custom_post->post_title;
$tag['labels'][] = $custom_post->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
In your form you can add the field:
[select* your-field-name include_blank]
In the example above the post_title is used in the options of the dropdown. You can add your own fields here (name, number, description, photo).
I do no think the wpcf7_form_tag works in the same way as vicente showed in his great answer before. It may have changed since 2015.
If you read here it explains how you need to use the wpcf7_form_tag: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
With that in mind along with this other post from Contact Form 7: https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351
I came up with this code to create a custom dropdown list for a custom post type that I have.
add_action( 'wpcf7_init', 'custom_add_form_tag_customlist' );
function custom_add_form_tag_customlist() {
wpcf7_add_form_tag( array( 'customlist', 'customlist*' ),
'custom_customlist_form_tag_handler', true );
}
function custom_customlist_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$customlist = '';
$query = new WP_Query(array(
'post_type' => 'CUSTOM POST TYPE HERE',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
));
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $post_title ), esc_html( $post_title ) );
}
wp_reset_query();
$customlist = sprintf(
'<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
$tag->name . '-options',
$customlist );
return $customlist;
}
Then you use the tag in contact form 7 like this.
[customlist your-field-name]
Hopefully this helps someone else who was looking for a way to do this like I was.
You could alter it to get any information you need from the custom post type.
It does not have any validation though.
Clyde Thomas code still works nice, thanks !
In my case I need the data from a plugin instead of a post so I've modified the code removing the WP_query and the while
global $wpdb;
$result = $wpdb->get_results("SELECT title FROM wp_asl_stores ORDER BY title ASC ");
foreach($result as $row) {
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $row->title ), esc_html( $row->title ) );
}

Gettin atrribute thumbnail from node from a wordpress rss feed

I've been trying to get this seemingly easy peace of code to work.
I'm loading rss from a wordpress site and it all works fine except for the thumbnails. Since in the XML they are set as an attribute instead of a nodeValue i can't seem to get import them. (i've really tried a lot)
$rss = new DOMDocument();
$rss->load('http://goalprogramme.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
// in XML it looks like <media:thumbnail url="http://goalprogramme.files.wordpress.com/2014/01/dsc_0227.jpg?w=150"/>
//echo $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url');
//push items
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'thumbnail' => $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url') // this line doesn't work !!!
);
array_push($feed, $item);
}
Any help would be greatly appreciated.
Thanks so much in advance!
Hours later i've created another piece of code that does work. If anyone needs it it, here it is:
$feed_array = array();
$feed = simplexml_load_file('http://goalprogramme.wordpress.com/feed/');
foreach ($feed->channel->item as $item) {
$title = (string) $item->title;
$description = (string) $item->description;
$link = (string) $item->link;
$date = (string) $item->date;
if ($media = $item->children('media', TRUE)) {
if ($media->thumbnail) {
$attributes = $media->thumbnail->attributes();
$thumbnail = (string)$attributes['url'];
}
}
$item = array (
'title' => $title ,
'desc' => $description,
'link' => $link,
'date' => $date,
'thumbnail' => $thumbnail
);
array_push($feed_array, $item);
}

Resources