Showing Related Posts Manually (Wordpress) - php

We have a custom post type in my wordpress website under which we publish our reports. Here's a sample report page -
goo.gl/hqVFJ2
See the report name. The report name has a specific format which is "Region" Report Topic "Market"
For each topic we have 5 regional variants, which are as follow:
Global
North America
Europe
Asia Paciic
LAMEA
Our requirement is, when someone opens any one of the variant the other 4 should appear under the similar reports section. For that matter I used Contextual Related Posts plugin which is not able to meet the requirement 100% and in fact to the contrary making the site heavy. I was trying to figure out if there can be a way to achieve this with a custom code.
I found a code snippet but that shows similar posts as per the tags.
Here's the code of the report page:
<h1 itemprop="headline" class="entry-title"><?php the_title(); ?> </h1>
<?php if (get_post_meta($post->ID, 'cc_price', true) !== '') { ?><span
class="price_meta">
<span class="price_left"></span><span class="price_center"><?php
if (cc_get_option('cc_currency') != '') {
echo cc_get_option('cc_currency');
} else {
echo get_option('currency_symbol');
}
echo get_post_meta($post->ID, 'cc_price', true);
?></span><span class="price_right"></span></span> <?php } ?>
<ul class="post_meta">
<li class="estimate"><?php echo get_post_meta($post->ID,'publish_date_new',true); ?><time class="dt-published" datetime="<?php echo get_post_meta($post->ID,'publish_date_new',true); ?>"></time></li>
<li class="cate"><?php printf(IN . ' ' . '%s', $taxonomies); ?></li>
<li itemprop="author" class="author p-author h-card">By <?php echo get_post_meta($post->ID,'published_by_new',true); ?>
</li>

What you could do is add a custom field to specify the Unique Id of the report.
Then display all posts with that Unique Id (except the one the user is on)
You can use Advanced Custom Fields to create the custom field.
Then your hook would look something like
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'your_custom_post_type',
'meta_key' => 'report_unique_id',
'meta_value' => the_field('report_unique_id'),
'post__not_in' => get_the_ID(),
));
if($posts)
{
foreach($posts as $post)
{
/* Your related Post */
}
}
?>

Related

Organize Term Categories Followed by ACF Relationship Custom Fields - Wordpress

I have two custom post type, the name is: "University" and "Majors".
Also, I have ACF relationship custom fields, the name is: "university_majors" with return format Post Object and Filter by Post Type: "Major".
And then, inside majors custom post type, I have taxonomy name: "major_categories".
The goal is, when I select the relationship fields inside university post type, I want it to be displayed with major categories followed by ACF relationship fields.
Example:
Faculty of Computer Science and Information Technology
-) Software Development
-) Artificial Intelligence
and more.
Here is the screenshoot for the wp-admin:
Above is the university post type.
And above this is relationship custom fields.
Above is major post type, and below is the categories:
Actually, I can order the categories followed with the relationship post type. But, the result like this:
The categories not in order correctly. The Artificial Intelligence should be below software engineer not duplicate categories.
I tried many things to solve, but not found any solutions.
Here is the code:
<div class="col-sm-8 position-sticky">
<h2>Jurusan Kuliah di <?php the_title(); ?></h2>
<p>Ada jurusan apa saja di <?php the_title(); ?>? Berikut adalah daftar jurusan yang tersedia:</p>
<?php
$university_majors = get_field('university_majors');
$previous_category = '';
foreach($university_majors as $university_major){
$categories = get_the_terms($university_major->ID, 'major_categories');
$c_terms = array();
foreach($categories as $term){
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
?>
<?php if($previous_category !== $unique_cat[0]) { ?><strong><?php $previous_category = $unique_cat[0]; echo $unique_cat[0]; ?></strong><?php } ?>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'majors',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array($university_major->ID),
'nopaging' => true
));
foreach($posts as $post):
setup_postdata($post);
?>
<li><?php the_title(); ?> </li>
</ul>
<?php endforeach; ?>
<?php
}
?>
</div>
Any help is very much appreciated. Thank you in advance, developers.

LearnDash - Display lesson list for current course, grouped by lesson category

I am trying to customize the LearnDash Course listing template, to be able to group my client's lessons by category. Currently, each course page lists the applicable lessons using the course/listing.php template_part and lesson/partials/row.php.
I commented out the leardash_get_template_part('lesson/partials/row/php') section in listings.php and replaced with this:
$terms = get_terms('ld_lesson_category');
foreach( $terms as $term ):
?>
<h3><?php echo $term->name; // Print the term name ?></h3>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'sfwd-lessons',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this taxonmy
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endforeach;
but that just lists the course name associated with the category instead of the lesson name. I also believe this omits some important data from the orig code, such as 'course_pager_results' which I'd rather not lose just by customizing the section.
Can anyone with LearnDash LMS experience help with this?

php generate links to all wordpress categories

Im having a little problem. I want to display all categories connected to a wordpress post.
I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url.
So i took the working parts from different places and put them together to the code below, and its working.
The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)
what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.
<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>” ><?php echo $category_name ?></a>
Please try this code
<?php
$category_args_query = array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 1,
'number'=> 10
);
$categories = get_categories( $category_args_query );?>
<ul>
<?php
foreach ( $categories as $category )
{ ?>
<li><?php echo $category->name; ?> </li>
<?php
}
?>
</ul>
If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.
function postsCategories()
{
$categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
$allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
if ( $categories_list )
{
printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after));
}
}
Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>
Actually just <?php the_category(', ') ?> should be enough (within the WP while loop). The , determines what should go in-between, if there is more than one category.

Custom Posts not showing in archive page

I currently have a custom post type of Episode with a taxonomy of podcast. When i run the following loop on my archive page i am seeing that I have 149 posts
<?php
$args2 = array(
'posts_per_page' => 1000,
'post_type' => 'episode',
'podcast' => 'my-episodes',
'post_status' => 'publish'
);
$posts = get_posts($args2);
$count = count($posts);
echo $count;
?>
However, when I'm running this variation of the loop on my archive.php file (copied from the wordpress.org page), i am only getting 130 posts
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The loop is saying that my most recent entry was from the 5th, however i have been creating posts on a regular basis over the last few days. Are there any tests I can run to see why the most recent posts are not showing up on the archive page?
Your code appears to be correct. Double check that the posts that are missing are published and not set as a draft/saved. Also check that your custom taxonomy and post types are correct for the query you are running.
After reviewing the dashboard of the site per the recommendation of Tom here is what I was able to find
The site has a plugin called WPML activated. This allows the site to create content in multiple languages.
When the plugin is initially activated, it will ask for the site's native language and apply that to the existing posts and custom posts. This way if you were on an englsih version of the site you would see content ABC while on a spanish version you would see content XYZ.
There is a separate section inside of a custom post that allows you to make said custom post translatable. If this is not checked then your custom post will not display on any version of the site. The reason being (at least in my 5 minutes of research) is that because there is no language specified, it will not load on the language specific versions of the site
I have done two things to fix this:
I have added the ability to make the custom post translatable,
There is a setting that will allow you to display all content that is not translated on any version of the site as opposed to not displaying any content
Thank you everyone for your help.

I can't properly load child posts of a post

I'm new to PHP and WordPress.
I'm using Types plugin for creating my custom types (more info at here).
Here is my code:
<?php
$args = array('post_type' => 'project-list');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
?>
<span><?php echo $post -> post_title; ?></span>
<ul class="project-list">
<?php
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
$mypostsChild = types_child_posts('project', $argsChild);
foreach ( $mypostsChild as $postChild ) :
?>
<li>
<p class="project-cost"><?php echo $postChild -> post_title; ?></p>
<span class="project-name"><?php echo $postChild -> fields['project-name']; ?></span>
<hr />
<p class="project-type"><?php echo $postChild -> fields['project-type']; ?></p>
<hr />
<span class="project-location"><?php echo $postChild -> fields['project-location']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
What I expect:
I need to load the list of projects by their type. For example I need the final output to look like this.
Project Group Name-1
proj-1
proj-2
proj-3
Project Group Name-2
proj-1
proj-2
proj-3
I've two problems here:
1: I don't know where to put the parent post "ID", and this code is now rendering me all the projects from all the project lists. I need to load projects separately for every project list (like above).
Q: How can I do this?
2: I can't sort the posts by their post-order (which is a custom field type). Thus, I noticed that the following part of the code is not working proper.
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
Q: How can I sort the child posts based on a custom field type, which is "post-order" in my case?
Thanks guys!
Posts haven't any parent or child. Posts are created under categories. You should create some categories and subcategory under category. i.e.
CATEGORY :Project Group Name-1
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
CAT:Project Group Name-2
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
Moreover, you have created custom post type. There are many ways to retrieve the terms of custom post types depending your spec. However, try the following code to make some sense:
<?php
//list terms in a given taxonomy
$taxonomy = 'project-list';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
Learn more about custom post type , terms and taxonomy. And read this link, you will get more codes what is related to your need. these will help you.
FYI: page has parent or child page

Categories