I am not expert on php and need some help about custom query on my wordpress author page.
I have no problem when using WP_User_Query, the problem is i need to combine it with $curauth
What i need to do is get authors list with WP_User_Query that filtered with current author page as a custom field value.
here is code that got error:
// WP_User_Query arguments
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$label = echo $curauth->user_login;
$args = array (
'role' => 'contibutor',
'number' => '10',
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
array(
'key' => 'user_label',
'value' => $label,
),
),
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
// do something
}
} else {
// no users found
}
I hope someone here can help to resolve my problem. Thank you
Try this
$curauth = ( isset( $_GET['author_name'] ) ) ? get_user_by( 'slug', $author_name ) : get_userdata( intval( $author ) );
$args = array (
'role' => 'contibutor',
'number' => '10',
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
array(
'key' => 'user_label',
'value' => $curauth->user_login,
),
),
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
// do something
}
} else {
// no users found
}
As pointed out by pieter-goosen in the comments, the line $label = echo $curauth->user_login; was causing the error message.
Related
First of all, sorry my horrible English =)
I'll try to explain how works everything:
I use this to create 'agreement' to be searched on my website, só the basic is the name, and where that 'agreement' works, for example, states and cities... but I have a problem when I try to select the 'states' and after show the cities to select where my 'agreement' can be used and save to show on the site.
But when I select the states, the WordPress just don't show the cities, and this problem happened after some att in WordPress plugins (exemple: Advanced custom field)
And know I have no idea how I can fix this.
Here is my code:
//Load cities - Convenio
function ag_apcef_load_cities_field_choices( $field ) {
// get selected
$selected = get_field( 'ag-estado-convenio', $post->ID );
// reset choices
$field['choices'] = array();
if($selected) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected->ID
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$field['choices'][$post->ID] = $post->post_title;
}
}
return $field;
}
add_filter('acf/load_field/name=ag-cidade-convenio', 'ag_apcef_load_cities_field_choices');
// Admin enqueue - Convenio
function ag_apcef_admin_convenio_enqueue( $hook ) {
// Allowed post types
$types = array( 'convenio' );
if( !in_array( get_post_type(), $types ) )
return;
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate-admin.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
)
);
}
add_action( 'admin_enqueue_scripts', 'ag_apcef_admin_convenio_enqueue' );
//Enqueue - Convenio
function ag_apcef_convenio_enqueue() {
if ( is_page( 'portal-de-vantagens' ) || is_page( 'bem-estar' )) {
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
}
}
add_action( 'wp_enqueue_scripts', 'ag_apcef_convenio_enqueue' );
// Load cities by state
function ag_apcef_city_by_state( $selected_state ) {
// Verify nonce
if( !isset( $_POST['populate_nonce'] ) || !wp_verify_nonce( $_POST['populate_nonce'], 'populate_nonce' ) )
die('Permission denied');
// Get selected state
$selected_state = $_POST['state'];
// Populate arr_data
$arr_data = array();
if(!empty($selected_state)) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected_state
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$arr_data[] = [
'key' => $post->ID,
'value' => $post->post_title
];
}
}
return wp_send_json($arr_data);
die();
}
add_action('wp_ajax_apcef_city_by_state', 'ag_apcef_city_by_state');
add_action('wp_ajax_nopriv_apcef_city_by_state', 'ag_apcef_city_by_state');
I have this snippet for creating a list of users sorted by the highest post views.
But I'm wondering of how can I create pagination for a custom array like that.
$topuser = array();
$avatar_size = 100;
$args = array(
'role__in' => array( 'contributor', 'author' ),
'hide_empty' => '1'
);
$users = get_users( $args );
foreach ( $users as $user ) {
$query = get_posts(
array(
'author' => $user->ID,
'cat' => '3',
'numberposts' => -1,
'post_type' => 'post',
)
);
$counter = 0;
$post_count = count_user_posts( $user->ID );
if ( ! $post_count ) {
continue;
}
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post->ID, 'post_views_count', true ) );
$counter += $views;
}
$topuser[] = array(
'id' => $user->ID,
'views' => $counter,
);
wp_reset_query();
}
// function to sort array based on views count
function sortViews( $a, $b ) {
return $b['views'] - $a['views'];
}
usort( $topuser, 'sortViews' ); // sort the array
$output = $topuser;
Thanks in advance!
I developed a plugin that reads a csv file in order to create 4 different custom taxonomies (Categories) and 1 custom post type (Post) using PHP and WordPress.
On the Initial import the execution time is less than 6 seconds, however the second time execution takes more than 1 minute in order to reset and re-upload the data.
The issue that I found is on the resetAllPosts function (see code)
public function resetAllPosts() {
$query = new WP_Query( array(
'post_type' => 'psp',
'post_status' => 'publish',
'posts_per_page' => 100
) );
// remove all taxonomies will take too long..
// delete all custom taxonomies terms
$terms_payment = get_terms( 'payment', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_country = get_terms( 'country', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_currency = get_terms( 'currency', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_provider = get_terms( 'provider', array( 'fields' => 'ids', 'hide_empty' => false ) );
foreach ( $terms_payment as $value ) {
wp_delete_term( $value, 'payment' );
}
foreach ( $terms_country as $value ) {
wp_delete_term( $value, 'country' );
}
foreach ( $terms_currency as $value ) {
wp_delete_term( $value, 'currency' );
}
foreach ( $terms_provider as $value ) {
wp_delete_term( $value, 'provider' );
}
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// remove the relation between the post and the taxonomies
//wp_delete_object_term_relationships(get_the_ID(),array("provider","payment","currency","country"));
// Remove the feature image from Media gallery
//$post_thumbnail_id = get_post_thumbnail_id( $post_id );
//wp_delete_attachment( $post_thumbnail_id, true );
// delete the post
wp_delete_post( $post_id, "true" );
}
echo "Reset of all data success..";
}
I would like to know the best/fastest way to delete all terms data.
I am needing some assistance. My goal is to query "clinics" post type by array of zip codes. Then get the ID's of those clinics and run another query of post type called "clinicspromo" to get those results. Then inside the loop, run query #3 to retrieve the clinic information again that is tied to each clinic promo. I have most of it completed, I am just having a problem turning the results of $post->ID; to an array of ID's separated by commas just like the zip code list. Any help would be appreciated!
$zipcodelist = '90001, 90002, 90003';
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
$postlist = get_posts( $args );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$argstwo = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinicpromos',
'meta_query' => array (
array (
'meta_key' => 'assignclinic',
'meta_value' => $current,
'compare' => 'IN'
)
) );
$the_query = new WP_Query( $argstwo );
Changed your foreach loop like: I think you have an extra + sign when storing the IDs.
This is your loop:
foreach ( $postlist as $post ){
$posts[] += $post->ID;
}
Replace it with:
foreach ( $postlist as $post ){
$posts[] = $post->ID;
}
Try this, you do not need +
foreach ( $postlist as $post ) {
$posts[] = $post->ID;
}
I'm using Barcelona theme and I need to get authors who have posted in certain category.
In my author.php template I have:
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count'
) );
<?php
foreach ( $barcelona_authors as $barcelona_author_id ) {
barcelona_author_box( $barcelona_author_id, false );
}
?>
How to get authors who have posted to category ID 59?
For example I tried with:
$barcelona_authors = get_posts('category=59');
But I'm getting error. Any help?
ERROR:
Notice: Object of class WP_Post could not be converted to int in
/home/wp-includes/author-template.php on line 296
Your get_posts( 'category=59' ) code should work. I have tested the following on one of my test wordpress installation and it works (with a different category ID of course). If you get an error during the get_posts call you need to show us the error.
<?php
$category_posts = get_posts( 'category=59' );
$authors_ids = array();
$authors = array();
foreach( $category_posts as $cat_post ) {
$authors_ids[] = $cat_post->post_author;
}
// Not sure if you need more data than just the ID so here is what you need
// to get other fields.
foreach( $authors_ids as $id ) {
$user = get_user_data( $id );
// Display name: $user->display_name;
// Nice name: $user->user_nicename;
// etc...
}
?>
This was the solution for Barcelona theme:
$barcelona_posts = get_posts('cat=59');
$barcelona_author_ids = array();
foreach ( $barcelona_posts as $k => $v ) {
if ( ! in_array( $v->post_author, $barcelona_author_ids ) ) {
$barcelona_author_ids[] = $v->post_author;
}
}
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count',
'include' => $barcelona_author_ids
) );