PHP sort in alphabatical order - php

So I have on my WordPress site a user page via a template.
the users are loaded via a custom post type called members.
now I have this part to display the users on a template page.
but I need it to be in alphabetical order
the code I have is as follows
TEMPLATE:
$member_type = (isset($member_type) ? $member_type : 'seb');
$post_type = $member_type == 'seb' ? array('companies', 'members') : 'companies';
// prepare arguments
$args = array(
'post_type' => 'members',
'orderby' => 'name',
'order' => 'ASC',
'post_status' => 'publish',
);
// create a query based on arguments
$query = new WP_Query($args);
while ($query->have_posts()):
$query->the_post();
plus I have:
<section>
<p>
<?php echo (get_field('title') ? get_field('title') . ' ' : '') ?>
<?php the_field('contactperson') ?>
</p>
</section>
with 'contactperson' being the member being called up and displayed
so how do I put the output of contact person in alphabetical order

I am assuming you are using Advanced Custom Fields, but any custom field manager plugin applies. Here is an explanation from the ACF website: https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/
Bottomline: you need to order by the meta value of your meta key, "contactperson".
$args = array(
'post_type' => 'members',
'meta_key' => 'contactperson',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish',
);

Related

Wordpress short code to display all child page by parents ID

I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here

How to render formatted post content in widget in Wordpress

I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?
please check whether you are getting any text in $post->post_content
by echo $post->post_content
All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}

Show category by latest post date

I am working in a project where I am using below code to limit number of posts in each category. But I can't figure out how to sort these categories by latest posts. My code is working fine but when I add argument in wp query array, it does not sort or it breaks. I want to order the category with most recent post (date).
Can anyone guide me?
<?php $terms=g et_terms( array( 'taxonomy'=>'category', 'hide_empty' => false, ) ); foreach($terms as $cat){ $cata_name = $cat->name; $term_id = $cat->term_id; ?>
<?php //echo '<h3>'.$catname[0]->cat_name.'</h3>'; ?>
<?php $catqueryy=n ew WP_Query ( 'cat='.$term_id. '&posts_per_page=5');$count=$ catqueryy->found_posts; ?>
You can add ORDER BY argument in wordpress as:
<?php
$args = array(
'post_type' => 'post', //your_post_type
'orderby' => 'date', //meta_value
'meta_query' => array(array('key' => 'cat','value'=>$term_id)),
'order' => 'DESC',
'posts_per_page' => 5,
);
$catqueryy = new WP_Query($args);
?>
You can explore the wordpress document: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

WordPress: How to sort content by ACF custom field?

With use of the Advanced Custom Fields plugin I created a select dropdown which contains 6 membership types. All of my 'listings' using this custom field are assigned one of the 6.
I'd like to display all 'listings' by:
Ultimate Plus
Ultimate
Professional
Commercial
Business
Free
In this particular order, so those paying for the highest level membership have their 'listing' appear at the top of the page.
I expected it to be similar to this which I just found but unsure exactly:
// args
$args = array(
'post_type' => 'directory_listings',
'meta_key' => 'free',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '#',
'value' => array( #, # ),
'compare' => 'IN',
),
),
);
// query
$wp_query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php
while( $wp_query->have_posts() ) {
the_post();
ldl_get_template_part('listing', 'compact');
ldl_get_featured_posts();
}
?>
<?php else : ?>
<?php endif; ?>
You are almost there
If you change the choices in the Advanced Custom Fields to
1 : Free
2 : Business
3 : Commercial
4 : Professional
5 : Ultimate
6 : Ultimate Plus
And then the default to 1
By doing this you are setting the values to the number and the default is the Free value. In the admin screen you will be presented with the text value.
Then to do your query try this for the query
$wp_query = get_posts(array(
'numberposts' => -1,
'post_type' => 'directory_listings',
'meta_key' => 'membership_type',
'orderby' => 'meta_value',
));
It will get all of the posts that have a value set and order it by the membership type descending which is what you want.
I have tried this on my local setup to confirm this.

WP Loop with custom array field as argument

I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)

Categories