Wordpress show (grand)parent, child and grandchildren pages - php

I have a brand page (as parent) with several products (children), product groups (also children) with products (grand children) as pages. I'm querying a lot which seems a bit ridiculous. Is there a better way to achieve this the following?
Brand (parent page)
Product (child page)
Product group (parent page)
Product (child page)
Product group (parent page)
Product (child)
Code I'm using to query the pages:
<?php
$args = array(
'cat' => 54,
'orderby' => 'menu_order',
'order' => 'ASC',
'hierarchical' => 1,
'post_parent' => $post->ID,
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'strips/card-product' );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent__in' => array($post->ID),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// INSIDE PRODUCT GROUP
if ( in_category('product-groep') ) {
$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent' => get_the_id(),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
// The Query
$query1 = new WP_Query( $args );
// The Loop
while ( $query1->have_posts() ) {
$query1->the_post();
if ( in_category('product-groep') ) {
$args = array(
'category__not_in' => array(54,8),
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent' => get_the_id(),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish',
'post__not_in' => array(692)
);
// The Query
$query2 = new WP_Query( $args );
// The Loop
while ( $query2->have_posts() ) {
$query2->the_post();
get_template_part( 'strips/card-product' );
}
wp_reset_postdata();
} else
{
}
}
wp_reset_postdata();
// OUTSIDE PRODUCT GROUP
}
else {
}
?>
<!-- END LOOP 1 -->
<?
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

Use this WP function get_pages() and get_page_children()
function get_child_pages( $parent_page_ID ){
$all_pages = get_pages( array( 'post_type'=> 'page' ) );
$child_pages = get_page_children( $parent_page_ID, $all_pages );
if( !empty( $child_pages ) ){
$html .= '<ul>';
foreach ( $child_pages as $key => $child_page ) {
$html .= '<li>'.$child_page->post_title;
get_child_pages( $child_page->ID );
$html .= '</li>';
}
$html .= '</ul>';
}
return $html;
}
function list_pages(){
$html = '';
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'page' ) );
$html.= '<ul>';
foreach ( $parent_pages as $parent_page ) {
$html .= '<li>'.$parent_page->post_title;
$html .= get_child_pages( $parent_page->ID );
$html .= '</li>';
}
$html.= '</ul>';
return $html;
}
add_shortcode( 'list_pages', 'list_pages' );

Related

How to display all posts assigned the same category?

I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?
Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}
You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}

Execute 1 query to get 3 posts from each category

I want to get 3 posts (if exists) from each category, and execute only one query to get all posts.
For example if I have 3 categories then I want to get a total of 9 posts from all categories.
Below is how I do using a loop to execute multiple queries:
$query = new WP_Query;
foreach ( $cats as $cat ) :
$query->query( array(
'cat' => $cat->term_id,
'posts_per_page' => 3,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
));
And I tried this:
$args = array(
'posts_per_page' => 3,
'order' => 'desc',
'post_type' => 'post',
'cat' => array(19,20,2,3),
'ignore_sticky_posts' => true
);
$args['orderby'] = 'post_views';
$posts = get_posts( $args );
I cannot figure how to get 3 posts if exists from each category or all existing categories.
As a result I get only 3 posts from first category
Try the following
$args = array(
'cat' => array(1,2,3),
'order' => 'ASC',
'showposts' => 3
);
query_posts($args);
<?php
$cat_args = array (
'orderby' => 'name',
'order' => 'ASC',
);
$threecategories = get_categories( $cat_args );
foreach ( $threecategories as $threecategory ) {
echo '' . $threecategory->name . '';
$post_args = array(
'posts_per_page' => 3,
'cat' => $threecategory->cat_ID
);
$threeposts = query_posts( $post_args );
while(have_posts()) : the_post();
echo '' . get_the_title(). '';
endwhile;
}
wp_query_reset();
May be this will help you

Get WooCommerce featured products with a custom shortcode

I tried to get array of featured products to use theme in my own plugin with jquery slider
I have made this function and get attrs from class-wc-shortcodes.php
but no results
add_shortcode('soqopslider', 'wps_soqopslider');
function wps_soqopslider() {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'featured_products' );
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
// The Query
$the_query = new WP_Query( query_args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo "No featured products found :(";
}
return "<span style='background:green;color:white;' >nothing</span>";
}
what I must add or change to get it works
I use it now as shortcode in welcome page just for testing
There was some errors in your code. So I have made the necessary changes.
Also the Shortcode data has to be returned not echoed.
Here is the functional code:
add_shortcode('soqopslider', 'wps_soqopslider');
function wps_soqopslider( $atts) {
$atts = shortcode_atts(
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'soqopslider'
);
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
// The Query
$the_query = new WP_Query( $query_args );
$html = '</ul>';
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$html .= '<li>' . get_the_title() . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
// Output
return $html . '</ul>';
} else {
return "No featured products found :(";
}
}
## BASIC USAGE: [soqopslider]
# ---- #
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and will output a list of feature products titles.

List all Woocommerce products by category

I'm trying to list all of the products and order them by category. Each product has just one category.
I got it to list all the products but on the one page. What I need is to make pagination work. Can anyone help me with this, please?
<?php
$taxonomy = 'product_cat';
$orderby = 'menu_order';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$order = 'DESC';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'menu_order' => 'asc',
);
$categories = get_categories($args);
foreach($categories as $category) {
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$args=array(
'post_type' => 'product',
//'product_cat' => $category,
//'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array($category->term_id),
),
),
'posts_per_page' => -1,
// 'category__in' => array($category->term_id),
'paged' => $paged,
'caller_get_posts'=>1
);
//$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$products_query = new WP_Query( $args );
//$product_array = get_posts($args);
//$temp_query = $wp_query;
//$wp_query = NULL;
//$wp_query = $products_query;
if ( $products_query->have_posts() ) :
while ( $products_query->have_posts() ) : $products_query->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile;
endif;
wp_reset_postdata();
} // foreach($categories

How to simplify multiple WP_Query with different meta values

Their are 6 total queries I need to use, below is list of 2 of them. Is it possible to simplify it? As you can see the key is the different values of meta_value, they are 1-6.
<?php
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 1
);
$the_query1 = new WP_Query($args1);
if ($the_query1->have_posts()) {
while ($the_query1->have_posts()) {
$the_query1->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
<?php
$args2 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => 2
);
$the_query2 = new WP_Query($args2);
if ($the_query2->have_posts()) {
while ($the_query2->have_posts()) {
$the_query2->the_post();
//do stuff
}
}
wp_reset_postdata();
?>
If you just want to get all the posts with meta values 1-6, you can pass as array
$args1 = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => array( 1,2,3,4,5,6 )
);
$the_query1 = new WP_Query($args1);
Edited as per comments:
You could add the values to an array and run a foreach loop
$meta_values = array( 1,2,3,4,5,6 );
foreach ( $meta_values as $meta_value ) {
$args = array(
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'product_column',
'meta_value' => $meta_value
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//...
}
}
wp_reset_postdata();
}

Categories