How to list all users of posted in wordpress - wordpress

$args = array(
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<ul>';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<li>'.$author->ID.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
I'm using post filter by post author. Above code displays all the users so need to display only authors who posted in the blog.
just like wp_list_authors() function but i need to get the author id intead of author name. because I need to create a dropdown list. While someone change the option, then I need to get the post by that author in AJAX

Update 1:
Hope this help:
$posted = get_posts([
'post_type' => 'your_custom_post_type',
]);
$author_ids = [];
foreach ($posted as $post) {
$author_ids[] = $post->post_author;
}
$author_ids = array_unique($author_ids);
if (!empty($author_ids) )
{
echo '<ul>';
foreach ($author_ids as $user_id)
{
$author_info = get_userdata($user_id);
echo '<li>'.$user_id.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
Make sure to change post_type to your post type and each user has both first_name and last_name.
There's a missing argument on Codex reference: has_published_posts.
$args = [
'orderby' => 'display_name',
'has_published_posts' => true
];
$authors = new WP_User_Query($args);
...
It's better to look for inside class file because info on Codex is not always up-to-date.

Related

Best way to display group of elements in wordpress

I've just started working on a project with wordpress. And I wonder what is the best practice to show a group of columns like this (highlighted in red)
in the page (post). I have a requirement that person who creating a post can insert default columns with different text and header.
I tried shortcode but it seems too complex. I want something like form with the input fields which will generate the colums. Thanks
I solve this problem with custom post types and a shortcode.
Plugins (optional - you could hand code everything)
Custom Post Types and Custom Fields creator - WCK
Intuitive Custom Post Order
Create your custom post type and fields.
Use Intuitive Custom Post Order to allow users to reorder the posts.
Customize this shortcode template to match your code and requirements:
add_shortcode('cpt-list', 'jpro_cpt_list');
function jpro_cpt_list() {
$args = array (
'post_type' => array( 'cpt' ),
'posts_per_page' => '-1',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$query = new WP_Query( $args );
$cpt_list = '<div class="jpro-cpt cpt-list container">';
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post = get_post($id);
$postID = $post->ID;
$image = get_the_post_thumbnail( $postID, 'medium', array( 'class' => 'aligncenter' ));
$name = get_the_title($postID);
$bio = apply_filters('the_content', $post->post_content);
if(!empty($image)){
$output_image = '<div class="cpt-list image">'.$image.'</div>';
} else {
$output_image = '';
}
if(!empty($name)){
$output_name ='<h3 class="cpt-name cpt-list">'.$name.'</h3>';
} else {
$output_name = '';
}
if(!empty($bio)){
$output_bio ='<div class="cpt-bio cpt-list">'.$bio.'</div>';
} else {
$output_bio = '';
}
$cpt_list .= '<section class="jpro-cpt cpt-list columnClass">';
$cpt_list .= $output_image;
$cpt_list .= $output_name;
$cpt_list .= $output_bio;
$cpt_list .= '</section>';
}
} else {
$cpt_list .='<p>Ooops! No cpt Found.</p>';
}
$cpt_list .='</div>';
wp_reset_postdata();
return $cpt_list;
}

Wordpress - Custom plugin to return related posts by category

I'm learning how to create custom plugins for Wordpress, I am trying to get related posts by category.
The problem is, I am returning all posts regardless of it's category whether it's the same category or not.
I've done a var_dump on the $categoriesIds[] and it is pulling the right category for each post.
I'm guessing something is not right with the WP_Query?
Can someone point out what is missing with the code?
function Add_related_posts($content) {
// If it's not a singular post, return the content
if (!is_singular('post')) {
return $content;
}
// Get post categories
$categories = get_the_terms(get_the_ID(), 'category');
$categoriesIds = [];
foreach ($categories as $category) {
$categoriesIds[] = $category->term_id;
}
$loop = new WP_Query(array(
'category_in' => $categoriesIds,
'posts_per_page' => 4,
'post_not_in' => array(get_the_ID()),
'orderby' => 'rand'
));
// If there are posts
if ($loop->have_posts()) {
$content .= 'RELATED POSTS:<br><ul>';
while ($loop->have_posts()) {
$loop->the_post();
$content .= '<li>' . get_the_title() . '</li>';
}
}
$content .= '</ul>';
// Restore data
wp_reset_query();
return $content;
}
Categories are pulled as expected. But in argument in WP_Query, you got one problem.
It should be category__in, NOT category_in.
Try this:
'category__in' => $categoriesIds,
See documentation: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

pods: Query customs posts filter by custom taxonomy

Using pods, I've created a new custom post type named staff_member which contains a field staff_department_relationship of type Relationship that is related to a custom taxonomy staff_department. Also note that the staff_department_relationship field is a single select of format dropdown.
Now I'd like to query all staff members from a particular department and have tried a lot of different things as per http://pods.io/docs/code/pods/find/ but without much success. I sometimes get a DB error or no posts at all:
$params = array(
'limit' => -1,
// 'where' => 'staff_department_relationship.staff_department = "some_custom_taxonomy_slug"'
'where' => 'staff_department.name = "some_custom_taxonomy_slug"'
);
$pods = pods('staff_member', $params);
Anyone with any idea what's going on?
Defines slug instead of name
'where' => 'staff_department.slug = "some_custom_taxonomy_slug"'
Code List post_type for taxonomy - PODS Wordpress
<?php
$params = array(
'limit' => -1,
'where'=>"TaxonomySlug.Slug = 'TaxonomyTermSlug'" ,
);
$pods = pods( 'NamePostypeHere' )->find( $params );
if ( $pods->total() > 0 ) {
while( $pods->fetch() ) {
//reset id
$pods->id = $pods->id();
//get the template
$temp = $pods->template( 'NameTemplateHere' );
//output template if it exists
if ( isset( $temp ) ) { ?>
<?php echo $temp; ?>
<?php }
}
//pagination
echo $pods->pagination();
}
else { echo 'No content found.'; }
?>

Plugin Development - wp insert post not working

I am new to plugin development for WP. I have a problem with wp_insert_post from the plugin file, tested running this plugin php file, and all the code before the wp_insert_post is fine, but once it jumps on wp_insert_post nothing happens after, stuck at 'Test15'. Tested running the insert post part of the code from test page within the theme folder and it worked. Not sure what is the problem here. Please see the code below.
function db_importer_insert_properties($properties, $category, $user_id, $post_type) {
echo 'Test9<br>';
$result = true;
echo 'Test10<br>';
if($properties) {
echo 'Test11<br>';
if(count($properties) > 0) {
echo 'Test12<br/>';
print_r($properties);
echo '<br/>';
$count = 0;
foreach($properties as $property) {
echo 'Test13<br/>';
$address =$property['address'] ; //get title
$building=$address['streetNumber'];
$street=$address['street'];
$suburb=$address['suburb'];
$state=$address['state'];
$postcode=$address['postcode'];
$title=$building. ' ' .$street.', '.$suburb.', '.$postcode.', '.strtoupper($state);
//test post
$new_post = array(
'post_title' => 'My post10 ',
'post_content' => 'This is my post10 ',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => 'uncategorized'
);
echo 'Test14<br/>';
print_r($new_post);
echo '<br/>';
echo 'Test15<br/>';
wp_insert_post($new_post);
echo 'Test16<br>';
if($post_id != 0) {
add_post_meta($post_id, "_bathrooms", esc_attr($property['features']['bathrooms']));
add_post_meta($post_id, "_bedrooms", esc_attr($property['features']['bedrooms']));
if(is_array($property['images'])) {
add_post_meta($post_id, "_images", esc_attr(implode("\n", $property['images'])));
}
else {
feedback("Post ID was 0");
}
feedback("added property $title with post_id $post_id");
$count++;
}
else {
feedback("post was failed to add");
}
}
feedback("Added $count properties");
}
else {
feedback("No properties to add.");
}
}
else {
feedback("No properties were selected");
$result = false;
}
return $result;
}
You forgot to declare your $post_id variable. Use the following:
$post_id = wp_insert_post( $new_post, true );
This will return the post ID on success or a WP error on failure.
Use print_r( $post_id ) to check the result.

Query blog roll of recent post by category from a website to another

Can you help me on this one?
The client wants the articles on the main blog site to be queried in a
sidebar of a landing page.
Is it possible? How?
Any answer is appreciated.
UPDATE:
$rss = new DOMDocument();
$rss->load('feed/url/goes/here');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'category' => $node->getElementsByTagName('category')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$cat = $feed[$x]['category'];
$cat_name = get_the_title();
if ($cat = $cat_name){
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('m / d / Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$date.'</strong><br />';
echo '<p class="smallbutreadable">'.$description.'</p>';
}else{
echo '';
}
}
I can retrieve the feed items, but not in a category. It must be in a category.

Resources