Wordpress - Imported pages appear, but lost data - php

I exported just the pages from one WP install to another and it was successful. When I go to the pages list, it displays them all and shows parent/child relationships, but doesn't seem to be recognized anymore when using some custom code in the template. It works on the original site just fine and I compared WP versions; they're the same.
Here is the code I'm using:
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'date', 'order' => 'ASC'));
// Get the page as an Object
$products = get_page_by_title('Products');
// Filter through all pages and find Products' children
$products_children = get_page_children($products->ID, $all_wp_pages);
// echo what we get back from WP to the browser
echo '<ul id="product-nav">';
foreach($products_children as $child){
echo '<li><a href="'.get_page_link($child->ID).'" title="'.$child->post_title.'">';
echo get_the_post_thumbnail($child->ID).'<span>'.$child->post_title.'</span>';
echo '</a></li>';
}
echo '</ul>';
When I performed a var_dump() on $all_wp_pages, they aren't being displayed there either. It's as if they don't exist, but show up on the back end as expected. Finally, I tried changing one of the child page's parent, then back again in hopes that would reset it, but had the same result.
Any idea why this would be happening?

Related

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

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!

Hot to add subcategories under category element in woocommerce flatsome theme WordPress

I'm trying to set up e-commerce website. Ran into a problem with Categories element.
I want the element to represent not only categories, but also subcategories below itself.
I also cannot edit page without UX builder, I think that's because Flatsome theme elements.
I want something like that :
Category_1 Category_2
Subcat1.1 ___Subcat2.1
Subcat1.2 ___Subcat2.2
Like that:
But, currently I can put categories only. Cannot insert subcategories below. Cannot change Category element.
Currently tried few examples here on stack, using php scripts, but failed.
I ask you for your help, because I don't now where to seek for info I need.
Is it even possible with given circumstances? Or I need to create whole Page without Flatsome, and then add custom element?
Here is the code to get parent and child category.
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_categories = get_terms('category',$parent_cat_arg);
foreach ($parent_categories as $category) {
echo '<h2>'.$category->name.'</h2>'; //Parent Category
$child_arg = array( 'hide_empty' => false, 'parent' => $category->term_id );
$child_cat = get_terms( 'category', $child_arg );
echo '<ul>';
foreach( $child_cat as $child_term ) {
echo '<li>'.$child_term->name . '</li>'; //Child Category
}
echo '</ul>';

Add page to menu programmatically after creation

I have been trying to figure this out for hours and I've had no luck. I know it's something easy that I'm missing, but I cannot find it anywhere on StackOverflow.
What I want to is add the page that is being created here to either the existing menu (which will be blank), or remove the current menu, create a new menu and add the page that way, whichever is easier.
Here is the code I'm using to add a page (and editing / saving) from a plugin interface:
$titlep2 = $_REQUEST['titlep2'];
$post_text2 = $_REQUEST['post_text2'];
if($titlep2 != ""){
$posthwe_id2 = get_option("hwepag2");
if($posthwe_id2 == ""){
$hwea2 = array('post_title' => $titlep2,
'post_content' => $post_text2,
'post_status' => 'publish',
'post_type' => 'page');
$post_idp2 = wp_insert_post($hwea2);
update_option("hwepag2",$post_idp2);
}else{
$my_post2 = array('ID' => $posthwe_id2,
'post_title' => $titlep2,
'post_content' => $post_text2,
);
wp_update_post( $my_post2 );
}
}
I have tried many variations of wp_update_nav_menu_item, but I cannot get it to work. I have an existing menu, but it will be blank when adding this post (this is for new/fresh sites that I develop offline). That being the case, it seems the menuID, which is "menu-top" does not exist in the UL before adding an element to the menu.
So, it's fine if the current menu needs to be deleted, a new one created, then the page I'm adding added to that menu.
Thank you so much!
Try this if not already done:
Go to Appearance > Menus > Select your menu if you have multiple of them > Check the checkbox down below that says "Automatically add new top-level pages to this menu"

Trying to display recent posts outside wordpress

I try to display recent posts on my main website which is outside wordpress, my blog is actually blog.domain.com and the directory root of the wordpress is in my main website if this help, here is my code:
// Include the wp-load'er
include('blog_subdomain/wp-load.php');
// Get the last 10 posts
// Returns posts as arrays instead of get_posts' objects
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 10
));
// Do something with them
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>';
But when I try to access my page where I want the recent post to be displayed it redirects me to the blog address.

Wordpress check if you're on the current page (not menu)

I am creating a little shortcode that acts like the custom menu widget, but I'm choosing the pages from a dropdown list, instead of creating a menus inside the wordpress (I'm also adding a color to it, so I can't just use regular wordpress menus widget).
So far so good, it's just one thing that's bothering me. I want to check if the page I'm on matches the one in my list (on the real page). I googled and searched a bit and found that with
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
I can get the current page I'm on (the link I see in my browser). Perfect!
So if I want to add a class called current page, all I need to see if my $actual_link matches with my selected link from the dropdown list.
I'm working this within a page bulder plugin The Creator, so I know how to create a working shortcode in it. My pages dropdown is created by making an array that will have page url as key and page name as a value. My loop is:
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->url] = $page->post_title;
}
}
And it works perfectly. Except that the url I get from this is of the form:
http://www.example.com/?page_id=150
Whereas I set my permalinks to be nice so the actual link in my browser is
http://www.example.com/my_page_name
The id is correct, and if I click on my 'menu' link I'll get to that page (desired result). But now I cannot just go and say:
$current = ($actual_link == $link) ? 'current_page' : '';
where $link is the variable that holds the link to the page from the dropdown, so that I can append this to my list to check if I'm on the current page (adds a current_page class). I need this class for the styling purposes - if I'm on the page that has this menu shortcode, next to the link that matches this page I'll get chevron (>).
So my question is, how to get the matching urls, no matter what permalink setting I use? Is there a way to specify this in the get_posts() query, or with the $_SERVER[] variable?
I am trying to avoid javascript with this one, and do everything server side.
You need to place get_the_ID() inside get_permalink() it will return the link which you have to match with $actual_link
$link = get_permalink(get_the_ID());
something like this.
Ok, found the answer.
I changed my query to
$args = array (
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$pages = get_posts($args);
$forms = array();
if(is_array($pages)){
foreach ($pages as $page) {
$forms[$page->ID] = $page->post_title;
}
}
So that I can get page ID, then use
$link_out = get_permalink($link);
$current = ($actual_link == $link_out) ? 'current_page' : '';
And it works! :D

Categories