Get Buddypress style to display posts in user's profile tab - php

I really hope you can help me here because it's literally driving me crazy and keeping me from sleeping for 3 days now.
I've been looking everywhere to find a solution to my questioning and everytime I think I'm finally getting somewhere the answers are either too old or the links down, or when someone is facing the exact same issue they never get an answer.
SO. Here is the thing.
In my function.php I created a CPT so my users can create posts with WP User Frontend without polluting my blogs. Now, since the posts are in a CPT, they no longer show up in the built-in Posts tab that Buddypress provides. So, I then added a custom tab to display their posts on their profile.
And now it's getting messy.
I managed to get the posts from the CPT, everything works fine (I know it because if I echo the post thumbnail and title I see them). But I can't figure out how to get the freaking Buddypress template to style the posts list. I think I've read something like more than a hundred threads, I went through Wordpress and Buddypress codex, I even dug into my Buddypress plugin files to find the templates and blog loops and try to understand how it works. But still, nothing.
Here is the code I have so far
function bp_costumes_tab_setup_nav() {
global $bp;
$parent_slug = 'profil-costumes';
bp_core_new_nav_item( array(
'name' => 'Costumes tab',
'slug' => $parent_slug,
'parent_url' => $bp->loggedin_user->domain . $parent_slug.'/',
'screen_function' => 'costumes_tab_show_screen',
'position' => 3,
'default_subnav_slug' => 'profil-costumes'
) );
}
add_action( 'bp_setup_nav', 'bp_costumes_tab_setup_nav' );
function costumes_tab_show_screen() {
add_action( 'bp_template_content', 'bptab_costumes_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function bptab_costumes_content() {
$myposts = get_posts( array(
'posts_per_page' => -1, // set the number of post to show, -1 if all
'author' => bp_displayed_user_id(),
'post_type' => 'cptgalerie',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
));
if( ! empty( $myposts ) ) {
foreach($myposts as $post) {
setup_postdata( $post );
**HERE I WANT THE POSTS FROM CPT TO BE DISPLAYED LIKE IN THE BUILT-IN POSTS TAB**
}
wp_reset_postdata();
} else {
echo '<div class="info" id="message">
<p>Aucun album publiƩ.</p>
</div>';
}
}
As I'm using Elementor Pro I also tried creating a loop and add the shortcode in my php with
echo do_shortcode ("[elementor-template ID='xxx']");
but it gets the template without the post info. It just keeps repeating the user's name (which is technically the "post title" since I used the "post title" widget in my template, I know, but it's not the title I want). Here too I tried to understand how it works by digging into another plugin I use that gets Elementor templates to display posts.
I found other answers where "if ( ! empty...)" is replaced by "if (have posts)... while" but it doesn't seem to work. I've read also that now the "bp_core_load_template" should be replaced by something else... many tests but no results. I also found that it has something to do with some archive template.
I'm lost.
Please, pleeeeeaaaase, can someone help me?
I need to sort it out, get it out of my mind and SLEEP.
Thank you!

Related

Gutenberg having conflict with my plugin short code and seems to be displaying on editor page

I have made my first plugin and it all works on the front end but I am having some weird backend conflict with the gutenberg. I have done two things, made my plugin automatically add the pages / overwrite them when they've checked off, and made the plugin shortcode.
Here is the problem on my gutenberg:
As you can see there are random red diamonds on the page. When I use the inspector I can see it has code from my plugin I created...
Here is how I created my plugin
First inside my functions page I registered a new short code.
add_shortcode('parts_listing_portal', 'portal_parts_list');
function portal_parts_list(){
include "shortcodes/parts_listing_shortcode.php";
return ;
}
Then I made content for inside the shortcode using JS, PHP, and HTML.(this is a big file with includes and API calls)
After the page looked the way I would like, I created a new function to work on my option submit. This function creates the page with the shortcode inside.
//Add parts
if ( get_option( 'parts' ) === "checked") {
$parts_list = array(
'post_title' => 'Parts',
'post_type' => 'page',
'post_name' => 'Parts',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[parts_listing_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0
);
// PARTS LIST
if(get_page_by_title('Parts') == null) {
wp_insert_post($parts_list);
}else{
$post_exists = get_page_by_title("Parts" );
$update_post = array('ID' => $post_exists->ID);
$post_update = wp_parse_args($parts_list, $update_post);
wp_update_post($post_update);
}
When my function adds to the page & you go to the gutenberg editor it shows the full page for a split second then goes away. I feel I missed a statement somewhere to only show on "page" but I have no idea where to look. I've spent a lot of time researching it but couldn't find it.
Thank you
Whenever adding a shortcode make sure to check iif in admin area. If in admin area return nothing
function portal_parts_list(){
if ( is_admin()){
return;
}else {
include "shortcodes/parts_listing_shortcode.php";
return ;
}
}
add_shortcode('part_description_portal', 'portal_parts_list');

Wordpress: Custom Post Type query not finding advanced custom field meta_query

I have a custom post type in Wordpress called People (each post in people is a person with some bio details etc).
Inside each People post I've created a custom field called 'tags' which has a series of checkboxes.
If someone clicks on the System Manager checkbox I want to be able to only pull through all the system managers on a particular page.
The most success I've had is using the wp_get_recent_posts instead of new wp_query .. this will output everybody UNTIL I add my extra meta query search. Then nothing appears.
$people_args = array(
'post_type' => 'people',
'post_status'=>'publish',
'meta_query' => array(
array(
'key' => 'tags', // name of custom field
'value' => 'systemmanager',
'compare' => 'LIKE'
)
)
);
$people_posts = wp_get_recent_posts($people_args);
foreach( $people_posts as $people ){
$fields = get_fields($people["ID"]);
if (get_post_thumbnail_id( $people["ID"] )>0) {
$myimgsp = wp_get_attachment_image_src(get_post_thumbnail_id($people["ID"]),'full');
echo '
<li class="item">
<img src="'.$myimgsp[0].'" alt="'.$people["post_title"].'" class="img-responsive" />
<div class="profile">
<h3>'.$people["post_title"].'</h3>
<p>'.$fields["job_title"].'</p>
View Profile
</div>
</li>';
}
}
wp_reset_query();
I've been stuck on this for hours trying out all the different types of loops but I've had no luck. Has anyone come across this problem before?
If you got stuck on the query maybe try different approach and use the post_id from the query and make small loop inside your code there use get_post_meta(post_id) to check the meta key and get the tags value... something like this...
$meta = get_post_meta($post_ID);
foreach($meta as $key=>$val)
{
if ($key == 'tags')
echo $val;
}
So I'm feeling a little silly! In my functions.php I had forgotten to set that the custom post type should support custom fields. So even though my Advanced Custom Fields were showing .. it wasn't getting pulled through when I was calling it.
To rectify I added to my custom post type config:
'supports' => array('title','thumbnail','custom-fields')

Wordpress - Static front page should show latest posts

I want to have a static front page on my WordPress and this static one should display the latest 5 posts and below that up to 6 custom meta fields. Thats why I changed the reading settings from last posts to "Static page", because otherwise I would not have the possibility to access the custom meta fields.
How can I now include the latest 5 blogposts on my front-page and use the default post template/output.
I did it with this query:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 5 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Do i now have to rewrite the code for the post here completly or can I just include the post template in some way?
Thanks!
You need to write this line to show latest posts:
<?php
wp_get_archives(array('type' => 'postbypost', 'limit' => 10, 'format' => 'html'));
?>
Check this link for more details
Instead of WP_Query you should try
wp_get_recent_posts( $args, $output );
Check more details here

Display latest articles inside of a post on wordpress

i was browsing some themes on wordpress, and i found one that is kind of a clone from 9gag.tv
You can see the demo theme here
http://codecanyon.net/item/youtube-viral-videos-9gag-tv-clone/full_screen_preview/6770578
If you see that page, you see the "latest videos" area below, that has the posts arranged by date, but when you open a video, that area changes and it gives you random videos.
Is there a way to arrange the videos like in the home page and allow user to browse to older videos?
I guess its easy to achieve it using the get_posts() loop. You'll also need to register new custom post_type for whatever posts you're going to post and fetch it through a loop like the example below.
I'm sure someone else will give a full answer but this will get you started.
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'post_type' => 'custom_post_type', 'posts_per_page' => 10, 'orderby' => 'rand' );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_attachment_link( $post->ID, false );
the_content();
}
wp_reset_postdata();
}
?>
You can use wordpress php function get_posts() to fetch the posts, then echo the data you want using a foreach. The function also has the offset argument which you can use for the navigation.

WordPress post per page (Archive)

i have a custom post type and would like to change the number of posts per page. i use the following code to show/limit the posts:
$args = array(
'post_type' => 'movie',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $wp_query->queried_object->taxonomy,
'field' => 'slug',
'terms' => $wp_query->queried_object->slug,
)
),
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
query_posts( $args );
for this time i have 20 posts with this post type and the default posts per page in wordpress admin (settings/read ...) is set to 10.
if i call the url without a page it shows 3 posts, if i call the url with "page/2/" it shows 3 posts but if i call the page with "page/3/" it shows nothing found ...
i wouldn't change the default value for the posts in the admin - does someone have a idea?
note: to "debug" i let me show the value for "$wp_query->post_count;" - this use the default admin posts per page - 10 ...
regards kai
When you load the page, it will do a query before the template file (archive.php) is called.
That first query will use the defaults set in WordPress; ie. 10 posts per page. By the sounds of things, you're running that second 'query_posts' call within archive.php, but it sounds like that is after you have run the appropriate checks.
Two possible solutions for you:
Modify the first query with the posts_per_page
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if( $wp_query->query_vars['post_type'] != 'movie' ) return;
$wp_query->query_vars['posts_per_page'] = 3;
}
Because you have modified a reference to $wp_query, you don't need to return it.
Run the query_posts call at the top of the archive.php template
Option 1 is what I would recommend - it's the most efficient, and the most appropriate.
The approved answer doesn't really work if the archive page is for a custom taxonomy. In that case, you might want to use the following:
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if(is_tax('movie-category')){
$wp_query->query_vars['posts_per_page'] = 10;
}
return $wp_query
}

Categories