WooCommerce category and sub category loops - woocommerce

When building a WooCommerce site they have made it really easy to display categories and sub categories on the archive and category pages.
However does anyone know if it's possible to add a the list of categories/sub categories to the (content-single-product) page template?
I have a store that had only a handfull of products and we would like users to be able to quick select from the side menu rather than go back and forward between the archive page and the product pages.
Thanks to anyone who can help.

$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
$woo_categories = get_categories( $prod_cat_args );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$return .= '' . $woo_cat_name . '';
}

$prod_cat_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
you can also edit all fields try this

Related

wp - all the pages by title - not just the first one

How can I get all pages that meet the criteria?
get_page_by_title()
The above only brings the first one. Is it possible for get_page_by_title to return an array of pages?
Here is the code to loop all pages with title:
<?php
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages( $args);
foreach ($pages as $page_data) {
$title = $page_data->post_title;
echo $title;
}
?>
You can change args to filter pages.

How to search for users in WP with serialized meta values

I can get users using $all_users = get_users(['meta_key' => 'first_name', 'meta_value' => 'john']); but I have a small problem now, as the meta value for feature I am trying to fetch is serialized.
I have copied a row from the table as shown below.
103 wp_capabilities a:1:{s:12:"cs_candidate";b:1;}
I can get the value and unserialize it, but not sure if there is a way to do this in WP.
UPDATE:
To add more clarification, I am referring to this page https://codex.wordpress.org/Function_Reference/get_users or the below function to be exact:
<?php $args = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => '',
'role__in' => array(),
'role__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
'date_query' => array(),
'include' => array(),
'exclude' => array(),
'orderby' => 'login',
'order' => 'ASC',
'offset' => '',
'search' => '',
'number' => '',
'count_total' => false,
'fields' => 'all',
'who' => ''
);
get_users( $args ); ?>
The 'meta_value' => '', part in the field is serialized a:1:{s:12:"cs_candidate";b:1;}
You can't search unserialize an search at the same time, I'm afraid. Searching in serialized data is always a bit of a hassle.
If you are certain of structure of the data you are searching, you could do something like:
$meta_args = [
'key' => 'wp_capabilities',
'value' => 'cs_candidate";b:1',
'compare' => 'LIKE'
];
$users = get_users(['meta_query' => $meta_args]);
It's anything but pretty, but should do the trick.

Create a filter in a page template

I am using Realto theme for wordpress I am wondering if I am on the correct path. I need to display properties from a city. So I decide to create a page template to add the location
This are the default arguments but I dont know how to filter the results by city
$args = array(
'numberposts' => '',
'posts_per_page' => $posts_per_page,
'offset' => 0,
'cat' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'property',
'post_mime_type' => '',
'post_parent' => '',
'paged' => $paged,
'post_status' => 'publish'
);
I tried adding
'locations' => 'MYCITY'
but it didnt work
This is an example of the search results when I search by city, so I am basing my arguments on this.
/?post_type=property&search_keyword=&locations=MYCITY&property_type=proyectos&beds=&baths=&status=&min-price=&max-price=
I assume that locations is a custom field? Maybe this works:
<?php
$args = array(
'post_type' => 'property',
'meta_key' => 'locations',
'meta_value' => 'MYCITY'
);
?>
Visit http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters for more details.

Wordpress Query_Posts Combine

I have the Wordpress theme called Clipper with Flatter as the child theme.
The theme uses query_output to output results in the homepage. My intention is to sort by custom fields, 'clr_up_vote'. However, this caused the outputs to be reduced to only posts with 'clr_up_vote' data and hid the rest.
I initially set out to fix by having two queries in index.php,
$ordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 1
, 'paged' => $page
, 'posts_per_page' => 10
, 'offset' => $offset
,'meta_key' => 'clpr_votes_up',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'clpr_votes_up',
'meta-value' => $value,
'value' => 1,
'compare' => '>='
),
array(
'key' => 'clpr_votes_down',
'value' => '',
'compare' => 'LIKE'
)
)
));
get_template_part('loop', 'coupon');
And,
$unordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 0
, 'paged' => $page
, 'posts_per_page' => 10-count($ordered_posts)
, 'offset' => $offset
, 'post__not_in' => $post_ids
));
get_template_part('loop', 'coupon');
But now the pagination doesn't seem to work. I tried to use WP_Query but the theme will only accept query_posts().
Any suggestions will be great. Thanks!
Remove meta query from query posts. Just orderby should be set to meta value and meta key should be set to the clpr_votes_up.
$ordered_posts = query_posts(array(
'post_type' => APP_POST_TYPE
, 'ignore_sticky_posts' => 1
, 'paged' => $page
, 'posts_per_page' => 10
, 'offset' => $offset
,'meta_key' => 'clpr_votes_up',
'orderby' => 'meta_value',
'order' => 'DESC'
));
And if you want all the posts then remove posts_per_page from the query.

Wordpress Admin Query for pages

I am trying to replicate the query that is used in the admin section to display the pages in the exact same order as there. However when I write:
$args = array(
'showposts' => '-1',
'post_type' => 'page',
'orderby' => 'menu_order',
);
I cannot retrieve the same order as in the admin. I assume there is an inner JOIN query to select based on the parent page and the menu order. I want to replicate the exact same order in a custom meta box and enable the user to say that they are related.
Can somebody help me to achieve this? If it is not possible I can use a custom query to select them directly from the db.
Thank you all in advance!
Maybe you should try to make something with the order param like this
$args = array('showposts' => '-1','order'=> 'ASC','post_type' => 'page');
<?php
$args = array(
'authors' => '',
'child_of' => 0,
'date_format' => get_option('date_format'),
'depth' => 0,
'echo' => 1,
'exclude' => '',
'include' => '',
'link_after' => '',
'link_before' => '',
'post_type' => 'page',
'post_status' => 'publish',
'show_date' => '',
'sort_column' => 'menu_order, post_title',
'title_li' => __('Pages'),
'walker' => ''
);
wp_list_pages( $args );
?>
http://codex.wordpress.org/Function_Reference/wp_list_pages
The important part of your question is sort_column. This example shows sorting (sort_column) as menu_order which will take the WP page order from the backend and sort them by that value. Editing this order is easy with plugins such as the Simple Page Ordering one.

Resources