Hi, Can anyone save my hair? How do I set a Query loop to only show the current users posts?
I’ve tried the meta query with author_name equals wp_user_login ( which both display the same value ) but can’t get it to work. Any hints?
Cheers
Alan
Hi, Can anyone save my hair? How do I set a Query loop to only show the current users posts?
I’ve tried the meta query with author_name equals wp_user_login ( which both display the same value ) but can’t get it to work. Any hints?
Cheers
Alan
You can try this tutorial Displaying current user posts with Query Loop
Let me know how it is working at your end.
Many thanks for the option. But why can’t this be done using the Meta Query? Maybe it needs the key to be dynamic too?
I confess I try and avoid any snippets ( I don’t edit function.php ever ) as it makes life much more difficult when trying to hand over to a new person. Much prefer everything to be native if possible.
Thanks again
Alan
Checked on my website, but this seems to work only for wordpress posts and not for CPT posts. Am I right or I am doing something wrong? Already changed the ’ $element_id’ to container ID, but it shows all the CPT posts and not only the posts of the current user.
Please could you take a look? Thanks!
I can’t get this to work with custom post types either. Does anyone know how to achieve this?
Here’s my code for CPT “turer”;
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id == 'ukujxp' && is_user_logged_in() ) {
$query_vars['post_type'] = [ 'turer' ];
$query_vars['author'] = wp_get_current_user()->user_id;
}
return $query_vars;
}, 10, 3 );
Yes. Here’s the code used for registering the CPT.
function cptui_register_my_cpts() {
/**
* Post Type: Turer.
*/
$labels = [
"name" => __( "Turer", "bricks" ),
"singular_name" => __( "Tur", "bricks" ),
];
$args = [
"label" => __( "Turer", "bricks" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => false,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"rest_namespace" => "wp/v2",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"can_export" => false,
"rewrite" => [ "slug" => "turer", "with_front" => true ],
"query_var" => true,
"supports" => [ "title", "editor", "author" ],
"show_in_graphql" => false,
];
register_post_type( "turer", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
User ID failed with previous code, the following work as intended;
add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
if ( $element_id == 'ukujxp' && is_user_logged_in() ) {
$query_vars['post_type'] = [ 'turer' ];
//$query_vars['author'] = wp_get_current_user()->user_id;
$query_vars['author'] = get_current_user_id();
}
return $query_vars;
}, 10, 3 );
Hi Alan,
Did you ever manage to make this natively?
My logic tells me that something like this could work, but no success so far.
Kind regards,
Kristoffer
thanks, all very easy in posts element enable Query editor (PHP) and:
return [
‘post_type’ => ‘event’,
‘posts_per_page’ => 10,
‘author’ => get_current_user_id(),
‘orderby’ => ‘ID’,
];