Wordpress Admin Query for pages - wordpress

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.

Related

Use ACF Taxonomy Field as Variable in Wordpress Custom Post Type Loop

I'm attempting to create a custom Wordpress query, using an Advanced Custom Fields field as a variable in the loop.
The use case is a page has a custom loop on it. For example, a page about a venue displays a loop of events (the custom post type) at the bottom of the page. I want for the person creating the page to choose what event tag (a tag taxonomy on the CPT) they want to attach to the page. Then the loop runs with that field attaching to the tag query used as a variable.
Here's my code so far:
<?php if ( get_field('event_tag') ) : ?>
<?php
$event_tag = get_field('event_tag');
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'tag_id' => $event_tag,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php echo $event_tag; ?><!-- This is only here to check the variable -->
<?php else : ?>
<?php
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true
);
?>
<?php endif; ?>
<?php $secondquery = new WP_Query( $args );
if ( $secondquery->have_posts() ) : while ( $secondquery->have_posts() ) : $secondquery->the_post(); ?>
I'm still wanting to sort by the event date, thus the meta_key and orderby. I'm not sure if that's affecting this. A couple things to note:
• That temporary line echoing the $event_tag does output the tag id (in this case 31).
• I've tried wrapping that tag_id in '', echoing it, etc. But it just displays nothing.
• Because this is querying a custom post type, I'm not sure if the standard tag even works. The tag is registered like this...if it matters.
// Taxonomy / Tags
function taxonomies_events_tags() {
$args = array(
'hierarchical' => false,
'label' => __( 'Event Tags','taxonomy general name'),
'singular_name' => __( 'Event Tag', 'taxonomy general name' ),
'rewrite' => true,
'query_var' => true,
'show_in_rest' => true
);
register_taxonomy( 'custom-tag', 'events', $args );
}
add_action( 'init', 'taxonomies_events_tags', 0 );
What do I need to change in my query to get the events in the specified tag to show, still ordered by event_start_date?
Thanks in advance.
You need to use a tax query to get events from a certain category. Assuming the $event_tag variable contains the tag id for the taxonomy term, the following piece of code should work:
$args = array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'ignore_sticky_posts' => true,
'tax_query' => array(
array(
'taxonomy' => 'custom-tag',
'field' => 'term_id',
'terms' => $event_tag
)
)
);

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

WooCommerce category and sub category loops

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

Woocommerce - Custom sorting and then ABC order

I have code that I am modifying inside of Up-Sells.php
I want to order by using a custom meta (OrderForProduct) and then I want to sort by ABC order. Does anyone know how to do this?
Right now when I add the meta query sorting, the products do not show if they do not have this custom value. I am looking to have it sort by the custom value and then have it sort by ABC order.
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'meta_key' => 'OrderForProduct',
'posts_per_page' => 50,
'orderby' => 'meta_value_num', 'order'=>'ASC',
'post__in' => $upsells,
'post__not_in' => array( $product->id ),
'meta_query' => $meta_query
);
$products = new WP_Query( $args );
What does "ABC order" mean? Are you talking about sorting by the product title? If so, I think you can do this:
'orderby' => 'meta_value_num title',
Although I've never tried it, that's how I understand it from the docos.
http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

How do you embed the Wordpress Categories widget in a page/post outside the sidebar?

Rather than use another widget I want to code the method to include the WP "Categories" widget directly within the content of a page or post.
Any ideas?
You can also use a plugin called Widgets on Pages and then just drag Categories into the newly created "sidebar" - then you just put the shortcode [widgets_on_pages] anywhere you want to use it.
To add wordpress post categories into the template file you need to use
<?php wp_list_categories($args); ?>
Default arguments are :
<?php $args = array(
'show_option_all' => ,
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => ,
'feed_type' => ,
'feed_image' => ,
'exclude' => ,
'exclude_tree' => ,
'include' => ,
'hierarchical' => true,
'title_li' => __( 'Categories' ),
'show_option_none' => __('No categories'),
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => 'Walker_Category' );
?>
or you can simply use it without parameters, in that case defaults will work.
<?php wp_list_categories(); ?>
you can read more about this function here
I think, you search for this.
https://wordpress.stackexchange.com/questions/36511/are-widgets-meant-to-be-used-outside-of-sidebars

Resources