Show posts by user - php

So, here is my php to show posts for wordpress:
<div class="rfp_hide" id="rhm_profile_item">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product, $post, $paged;
?>
<div class="rhm_post_container">
Posts go here
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
It shows all the posts regardless of who posted.
Each post has its href as following:
<a class="royal_author_link" href="<?php echo $userpro->permalink( $post->post_author ); ?>">
On the post author page, the permalink has following structure :
example.com/profile/someone.
Now, how can I modify it so that when I am in a specific user page, it only shows the posts by that user only?

Did you use a custom plugin to create the profile page?
In case you have access to the user ID related to the page, you can extend the query argument by the attribute "author", like so:
$args = array(
'author' => $yourUserID
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 20,
'orderby' => 'date',
'order' => 'DESC'
);

Related

Wordpress custom query: 'orderby' => 'date' not working when using multiple post types

In a Wordpress page template, I set up a WP custom query which queries a custom post type named "recipe" AND the regular posts as below. This works, but 'orderby => 'date' in there doesn't work: The page first lists the regular posts ordered by date, then the recipes by date. But I need ALL of them together (i.e. mixed) ordered by date.
Here's the definition of my custom query:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...followed by the loop...]
I am grateful for any hints what I can do to achieve the desired ordering.
I found a solution myself:
It works when I add remove_all_filters('posts_orderby'); before the custom query is defined. Obviously this resets any other ordering and allows the 'orderby' => 'date' to function as expected. Complete code:
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$number_of_posts = get_option('posts_per_page', 12);
remove_all_filters('posts_orderby'); // ADDED
$args = array(
'post_type' => array('recipe', 'post'),
'post_status' => 'publish',
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'ASC',
'paged' => $paged
);
$my_loop = new WP_Query($args);
[...]
Q1. what template file are you using?
The query below successfully merges the 2 post types together by date.
<?php
$args = array(
'post_type' => array('my_custom_post_type', 'post'),
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
// loop your content here
<?php include(locate_template('templates/content.php')); ?>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Wordpress show posts from custom post type with a category

I am using the CPT UI plugin which i have created a custom post type with (called Knowledgebase and the taxonomy called knowledgebase-categories)
i am using this code to display posts:
<?php $query = new WP_Query( array('post_type' => 'knowledgebase', 'posts_per_page' => 20, 'category_name' => 'Cisco' ) ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</article> <!-- .et_pb_post -->
<?php endwhile; ?>
it works fine without the category_name but with the above code its not showing any posts
there are posts with a category of Cisco
May this will Help you
<?php $query = new WP_Query( array(
'post_type' => 'knowledgebase',
'cat' => 5, // Whatever the category ID is for your aerial category
'posts_per_page' => 10,
'orderby' => 'date', // Purely optional - just for some ordering
'order' => 'DESC' // Ditto
) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
Here you can achieve by this
$args = array(
'post_type' => 'knowledgebase',
'tax_query' => array(
array(
'taxonomy' => 'knowledgebase-categories',
'field' => 'slug',
'terms' => 'knowledgebase-terms',
),
),
);
$query = new WP_Query( $args );
because they are posts from a custom post type i had to change category_name to the taxonomy (which is knowledgebase-categories)

How do I add order & orderby parameters to this wordpress query?

I managed to paint myself into a corner when using this snippet, but I can't manage to work out how to sort the query. Usually I can do it but with this snippet that excludes one tag on the tag page I can't really work it out. Anyone?
$exclude_tags = array(17);
global $wp_query;
$wp_query->set('tag__not_in', $exclude_tags);
$wp_query->get_posts();
if (have_posts()) : while (have_posts()) : the_post();
This example will be help full for you :-
$args = array(
'post_type' => 'post',
'meta_key' => 'pb_issue_featured',
'orderby' => 'meta_value',
'order' => 'DESC',
'posts_per_page' => $posts,
'paged' => $paged,
'paged' => 1,
'meta_query' => array(
array(
'key' => 'headline',
'value' => 1,
'compare' => '!='
)
)
);
add_filter( 'posts_orderby', 'filter_query' );
$q = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_query' );
function filter_query( $query ) {
$query .= ', wp_posts.menu_order ASC';
return $query;
}
Referenced From
Please look at this example. You can do like this.
The code will display the title of last ten posts sorted alphabetically in ascending order.
<?php
$args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Please refer the link http://codex.wordpress.org/Template_Tags/get_posts for more details.

Displaying post with certain key value

I have a wordpress function that displays all posts of a custom meta.
PHP:
<?php
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
echo get_the_ID();
endwhile;
?>
This displays 4 posts per page. However, I only want to display those posts whose $key value is dogs.
Hope this helps.
$args = array(
'post_type' => 'todo_listing',
'posts_per_page' => 4,
'order' => 'asc',
'meta_value' => 'dogs'
);

Trying to reorder posts by event date

I'm using "the events calendar" plugin in WP which has a feature to allow event posts to show up as regular posts. I currently have a slider on my homepage that I would like to show featured events on. I was able to get it to show events as a "featured" category, but am having trouble ordering it by event date instead of publish date. This is what I have now.
Here is the original code just calling the specific category posts
<?php query_posts ('category_name=' .$slide.'&posts_per_page='.$bvkPP.'&paged='.$paged ); ?>
This is what I changed it to
<?php query_posts( array ('category_name=' .$slide.'&posts_per_page='.$bvkPP.'&paged='.$paged, 'orderby' => 'meta_value','meta_key' =>'_EventStartDate','order' => 'ASC',) ); ?>
This affectively ordered it by event date, but has overridden the category and is just calling for all events. Any thoughts on how to get just the specific category to show up?
Thanks!
I finally got it figured out. Here's what I came up with.
<?php query_posts( array(
'category_name' => $slide,
'posts_per_page' => $bvkPP,
'paged'=> $paged,
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'order' => 'ASC',
'eventDisplay'=> 'startDate',
'post_type'=> 'tribe_events' ) ); ?>
Thanks for the help!
get all event using your cat id.
<?php
$paged = ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' => '17' //17 is cat id
)
),
'eventDisplay'=>'startDate',
'post_type'=>'tribe_events',
'orderby'=>'meta_value',
'meta_key'=>'_EventStartDate',
'posts_per_page' =>10,
'paged' => $paged,
'order' => 'ASC',
);
query_posts($args);
while ( have_posts() ) : the_post(); ?>
//your code here for get title/content
<?php
endwhile;
wp_reset_query();
?>

Categories