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
Related
I wanted to print tag list for my particular post type page. I used every function to fetch tags. but it is giving me all the tag list including used in other posts types.
<!-- begin custom related loop, isa -->
<?php
// get the custom post type's taxonomy terms
$custom_taxterms = wp_get_object_terms( $post->ID, 'your_taxonomy', array('fields' => 'ids') );
// arguments
$args = array(
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'posts_per_page' => 3, // you may edit this number
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'your_taxonomy',
'field' => 'id',
'terms' => $custom_taxterms
)
),
'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();
?>
For creating filter type of widget. this must be required to fetch tag list as per the post types.
for that, I created one generic function by you can list tags or any taxonomy list as per the post type.
function get_terms_by_post_type( $taxo_term, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT term.*, COUNT(*) from $wpdb->terms AS term
INNER JOIN $wpdb->term_taxonomy AS texo ON term.term_id = texo.term_id
INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = texo.term_taxonomy_id
INNER JOIN $wpdb->posts AS post ON post.ID = tr.object_id
WHERE post.post_type IN('%s') AND texo.taxonomy IN('%s')
GROUP BY term.term_id",
join( "', '", $post_types ),
join( "', '", $taxo_term)
);
$results = $wpdb->get_results( $query );
return $results;
}
Call this Function and pass taxonomy term and post type. it will give you an array of tags.
All,
I'm running a search with Wordpress and it's returning ACF data. The problem, is that for fields that are hidden with conditional logic, they are still returning in the search results.
Does anyone happen to know how to exclude fields that are hidden? Or, does anyone know those items are declared as hidden in the database? I can't seem to find anything that ties a field to another in the wp_postmeta.
EDIT: added some code by request
<?php
error_reporting(0);
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search = new WP_Query(
$search_query,
array(
'post_type' => 'page',
'post_status'=>'publish',
'post__in' => array(),
//'posts_per_page' => -1,
'paged' => $paged
)
);
$resultArray = array();
if (have_posts()){
while(have_posts()): the_post();
//places the WP post meta data into an array
$array = get_post_meta($post->ID);
//Flattens the array into one-dimensonal
$result = call_user_func_array('array_merge', $array);
//changes the search term to all lower case
$searchword = strtolower($search_query['s']);
//Searches the $result array for the $searchword and saves all occurances into $matches array
$matches = array_filter($result, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", strip_tags($var)); });
//Grab the first value of the $matches array since we won't always know the key
$first_value = reset($matches);
if(!empty($first_value)){
array_push($resultArray,
array(
'id'=>$post->ID,
'match' =>strip_tags($first_value)
)
);
}
endwhile;
}
$total_results = count($resultArray);
?>
I'm using a custom post type to hold products, each product holds a name via a custom field. When I display the product via its singular template it would be nice to have a next & previous link to go through those products, but it should go through them in order of their name. Currently that’s not possible as they would be ordered by their publishing dates.
So I added the following code in functions.php, but it's not working, I don't know if this has to be made in a different way:
if(is_singular('products')) {
add_filter('get_previous_post_join', 'rt_post_join');
add_filter('get_next_post_join', 'rt_post_join');
add_filter('get_previous_post_where', 'rt_prev_post_where');
add_filter('get_next_post_where', 'rt_next_post_where');
add_filter('get_previous_post_sort', 'rt_prev_post_sort');
add_filter('get_next_post_sort', 'rt_next_post_sort');
}
function rt_post_join($join, $isc, $ec) {
global $wpdb;
$join = " INNER JOIN $wpdb->postmeta AS pm ON pm.post_id = p.ID";
return $join;
}
function rt_prev_post_where($w) {
global $wpdb, $post;
$prd = get_post_meta($post->ID, 'data_product_name_product', true);
$w = $wpdb->prepare(" WHERE pm.meta_key = 'data_product_name_product' AND pm.meta_value < '$prd' AND p.post_type = 'products' AND p.post_status = 'publish'");
return $w;
}
function rt_next_post_where($w) {
global $wpdb, $post;
$prd = get_post_meta($post->ID, 'data_product_name_product', true);
$w = $wpdb->prepare(" WHERE pm.meta_key = 'data_product_name_product' AND pm.meta_value > '$prd' AND p.post_type = 'products' AND p.post_status = 'publish'");
return $w;
}
function rt_prev_post_sort($o) {
$o = "ORDER BY pm.meta_value DESC LIMIT 1";
return $o;
}
function rt_next_post_sort($o) {
$o = "ORDER BY pm.meta_value ASC LIMIT 1";
return $o;
}
And in my single page of products (single-products.php) I added the following code to display the pagination links:
<?php next_post_link('%link', 'Next product'); ?>
<?php previous_post_link('%link','Previous product'); ?>
Thanks.
What you actually need is a custom Query. You can try this (not tested tho):
Place this in your functions.php:
function getProductsQuery() {
return new WP_Query(array(
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => 'title',
'order' => 'ASC'
));
}
Then, replace your loop with the following:
<?php $products = getProductsQuery(); ?>
<?php while($products->have_posts()): $products->the_post(); ?>
<h2><?php the_title(); ?></h2>
(...)
<?php endwhile; ?>
Finally I solved it!
I just used the plugin Next/Previous Post Link Plus for WordPress and added the following code in my single page of products (single-products.php):
<?php next_post_link_plus( array(
'order_by' => 'custom',
'meta_key' => 'data_product_name_product',
'link' => 'Next product',
'format' => '%link'
)); ?>
<?php previous_post_link_plus( array(
'order_by' => 'custom',
'meta_key' => 'data_product_name_product',
'link' => 'Previous product',
'format' => '%link'
)); ?>
I'm trying to get all the comments from a post by an array of users.
This is what I'd like to be able to do:
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
$args = array (
'number' => -1,
'user_id' => $user_ids,
'post_id' => $post_id,
'status' => 'approve',
'order' => 'DESC'
);
$comments = get_comments( $args );
Now obviously this doesn't work, but that's what I'd like to do. Is there any other way to achieve this? Maybe using a custom select?
I've built a WPDB query based on the query method of WP_Comment_Query class. And doing the sanitization based on this forum post.
global $wpdb;
// Sanitize
$post = '1148';
$post = absint($post);
// Sanitize
$a = '2'; // User One
$b = '3'; // User Two
$user_ids = array_map( 'absint', array( $a, $b ) );
$user_ids = implode( ', ', $user_ids );
$query = "SELECT * FROM $wpdb->comments
WHERE comment_post_ID = $post
AND user_id IN ($user_ids)
AND comment_approved = 1
ORDER BY comment_date DESC";
$comments = $wpdb->get_results( $query );
A simple SELECT query would do:
$query = "SELECT * FROM $wpdb->comments
WHERE comment_post_ID = %d
AND comment_approved = 1
AND user_id IN %s
ORDER BY comment_date DESC"; // get only approved comment and sort by date
$comments = $wpdb->get_results($wpdb->prepare(
$query,
intval($post_id),
'('.implode(',', array_map('intval', $user_ids)).')'
)); // use prepare to prevent SQL injection
Hope this helps.
We can use the OR condition in MySQL to do this.
If I was to write the query we want using the values you provided it could be done something like this:
SELECT * FROM comments WHERE comment_post_ID='57' AND (user_id='10' OR user_id='22' OR user_id='41' OR user_id='80')
Now, we can make this dynamic and processable by PHP:
// The User IDs and Post ID (make sure you are escaping these properly).
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
foreach($user_ids as $uid){ $user_ids_string .= " OR user_id='$uid'"; } // Loop through each user and append their ID to the query segment.
$user_ids_string = substr($use, 4); // Remove the unnecessary first " OR "
$query = mysql_query("SELECT * FROM comments WHERE comment_post_ID='$post_id' AND ($user_ids_string)"); // Create the final MySQL Query.
while($row = mysql_fetch_array($query)){
// Do something with each $row[].
}
Before you use this, make sure you're connected to the WordPress database properly before using this and that all the tables and fields I've listed are correct for your installation first.
paste this code in functions.php
function users_comment( $postid = null , $users = array() ){
if( ! empty( $users ) && $postid ){
foreach ($users as $user) {
$args = array (
'number' => '',
'user_id' => $user,
'post_id' => $postid,
'status' => 'approve',
'order' => 'DESC'
);
$comments[] = get_comments( $args );
}
return $comments;
}else{
return 'Please provide a user id and postid';
}
}
use this function anywhere you want by calling it required parameters user ids and post id.
print_r( users_comment(1,array(1,4,3)) );
only single user to get post:
$args = array(
'status' => 'approve',
'number' => '-1',
'post_id' => 57,
'user_id' => 1,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;
?>
For multiple user get comment using post id:
$args = array( 'user_id' => 0 );
add_filter( 'comments_clauses', 'custom_comments_clauses');
$comments = get_comments( $args );
remove_filter( 'comments_clauses', 'custom_comments_clauses');
function custom_comments_clauses( $clauses ){
$clauses['where'] = str_replace( 'user_id = 0',
'user_id IN (1, 2, 3)',
$clauses['where'] );
return $clauses;
}
https://wordpress.stackexchange.com/questions/105010/get-comments-only-for-certain-specific-users-in-template-file
As Brasofilo already provided you the custom query to get the comments but it will fetch all the comments while they were trashed
$user_ids = array(10, 22, 41, 80);
$post_id = 57;
global $wpdb;
$comments=$wpdb->get_results("SELECT * FROM `wp_comments` WHERE
`comment_post_ID` =$post_id AND `user_id` IN (".join(',',$user_ids)")
AND `comment_approved` ='1' ORDER BY `comment_date` DESC");
I'm struggling to get the list of authors on the category page. I need to list the authors who have written at least 5 articles in that category.
I need something like this in tags also.
Any idea how to do this in wordpress?
Its only possible with a custom SQL query so here is a simple function which should return an array with user id's of users with at least $n posts in the current term archive, which means it should work in categories, tags and custom taxonomies
function get_authors_with($num = 5){
global $wpdb;
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$sub_q = $wpdb->prepare("SELECT * FROM $wpdb->posts
INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE ($wpdb->term_taxonomy.term_id = %s
AND $wpdb->term_taxonomy.taxonomy = '%s'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish')",
$current_term->term_id,
$taxonomyName
);
$sql = $wpdb->prepare("SELECT $wpdb->posts.post_author, FROM (%s)
GROUP BY $wpdb->posts.post_author
HAVING count(*) > %s",
$sub_q,
$num
);
return $wpdb->get_results($sql);
}
Try this.
function list_author_in_this_cat ($with) {
if (is_category()) {
$current_category = get_query_var('cat');
$args = array(
'numberposts' => -1,
'category' => $current_category,
'orderby' => 'author',
'order' => 'ASC'
);
} else {
$tag_id = get_query_var('tag_id');
$args = array(
'numberposts' => -1,
'tag__in' => $tag_id,
'orderby' => 'author',
'order' => 'ASC'
);
}
$cat_posts = get_posts($args);
$author_id_array = array();
$user_posts = array();
foreach( $cat_posts as $cat_post ):
$user_posts[$cat_post->post_author][] = $cat_post->ID;
endforeach;
foreach( $user_posts as $key => $user_post ):
$user_post = array_unique($user_post);
$count_user_posts[$key] = count($user_post);
if ($count_user_posts[$key] >= $with) {
$author_id_array[] = $key;
}
endforeach;
return $author_id_array; }
In your theme files, place this code wherever you want the list of authors to be displayed:
if (is_category() || is_tag()) {
$at_least = 5; // at least 5 articles in current category or tags
$author_array = list_author_in_this_cat ($at_least);
foreach (array_slice($author_array, 0, 4) as $author) : // limit 4 results
$name = get_userdata($author)->display_name;
$link = get_userdata($author)->user_login;
echo "<a href='/author/".$link."'>".$name."</a>\n";
endforeach;
}
Add the following code to your theme's function.php
function get_authors_in_category ( $category_id, $min_posts ) {
$posts = get_posts( array( 'numberposts' => 1000, 'category' => $category_id ) );
$author_count = array();
foreach ($posts as $post) {
if( array_key_exists( get_the_author_meta( 'display_name', $post->post_author ), $author_count ) ) {
$author_count[get_the_author_meta( 'display_name', $post->post_author )]++;
} else { $author_count[get_the_author_meta( 'display_name', $post->post_author )] = 1; }
}
$authors = array();
foreach ( $author_count as $author_name => $count ) {
if ( $min_posts <= $count ) {
$authors[] = $author_name;
}
return $authors;
}
This function as you can see returns an array of authors who have a minimum number of posts in a category. Category ID has to be passed, this can be obtained using the wordpress function get_cat_ID.
You can call this function from any where in the theme and display the authors as you wish.
Note: This is a more resource intensive than the custom SQL functions. Also this function might have some errors, I typed it here and didn't test it.
I did some simple coding with query post method.,
<?php
$cur_cat_id = get_cat_id(single_cat_title("",false));
$posts=query_posts("cat=$cur_cat_id&order=ASC");
foreach($posts as $post){
$number_of_posts = number_format_i18n(get_the_author_posts($post->post_author));
if($number_of_posts>5)
{
echo the_author_meta('user_nicename',$post->post_author)."(".number_format_i18n(get_the_author_posts($post->post_author)).")<br>";
}
}
?>
I think this may help you to resolve your problem.