List posts from category name. Almost – it lists all post content as well, help.
-
Hi
I’m trying to list all posts that belong to a category name generatate by sanitize_title function.
I almost achieved the result, but the code also displays whole post content.
<?php get_posts('category_name='.sanitize_title($current_user->membership_level->name).'&post_status=publish,future');?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile; else: endif; ?>
How to display only posts titles and permalinks? (without posts content).
All the best
Tom
-
Hey,
Haven’t tested this, but this should be a solid starting point for you:
<?php $sanitized_title = sanitize_title($current_user->membership_level->name); if( $sanitized_title ) { $args = array('post_type' => 'any', 'category_name ' => $sanitized_title); $query = new WP_Query( $args ); if( $query->have_posts() ) { while( $query->have_posts() ) { $query->the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php } wp_reset_postdata(); } } ?>
Tom
Hey
Thanks for the input ?? It is very helfpul. I’m struggling with the function second day.The code almost works, however quite strangely: it outputs all the posts no matter in which category. I’ve tried to change some bits here and there but with no effect.
Here is the code:
<?php $sanitized_title = sanitize_title($current_user->membership_level->name); if( $sanitized_title ) { $args = array('post_type' => 'post', 'category_name ' => $sanitized_title); $query = new WP_Query( $args ); if( $query->have_posts() ) { while( $query->have_posts() ) { $query->the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php } wp_reset_postdata(); } } ?>
How could I display only the posts from the category name slug stored in the sanitize_title variable? I’ve echoed the variable and the s?ug looks OK.
You have an extra space between “category_name” and the closing quote. As WP does not recognize this parameter with a space, it is ignored.
Guys, you have saved me at least dozen of hours trying to figure it out. Thank you.
If you have a paypal account, send me a pm with a link for a beer donation ??I hade to go back here for a moment. The function occurred not to work as I wish it would.
The problem is with $sanitized-title. It can store more than one category name. When that happens, the code returns blank page.
However, I’ve received a hint that the categories are returned by another function called
pmpro_getMembershipCategories()
. Could you help me modify the code to use that function? I’m not sure where and how to place it to make it work.
Multiple categories can be handled from either source. Which ever source you want to use, we need to know how the data is organized. We can get this if you
var_dump()
the returned value from both single and multiple category memberships and post the results.Also, how should multiple categories be handled? Query for the first? Last? Any one of them? Only posts with all?
If you want to use the pmpro function, do something like this:
$sanitized_title = pmpro_getMembershipCategories(); echo '<pre>'; var_dump($sanitized_title); echo '</pre>';
in place of the current assignment.
The echo – var_dump -echo line is deleted once you’ve collected the data.
I’ve tried that, but I must be doing something wrong. Below is the story and the result.
The tip I’ve talked about earlier:
I would like to display posts from categories to which a member has an access to on his profile page.
So, my goal is to display all the posts from categories to which a member has an access.
You would hook into the profile page, and check his level using the function pmpro_hasMembershipLevel() and the seeing which categories that level has access to by using pmpro_getMembershipCategories() and then displaying them.
So here is my final function:
<?php $member_level = pmpro_hasMembershipLevel('$current_user->membership_levels', '$current_user->ID'); echo '<pre>'; var_dump($member_level); echo '</pre>'; $sanitized_title = pmpro_getMembershipCategories($current_user->membership_level->ID); echo '<pre>'; var_dump($sanitized_title); echo '</pre>'; if ($sanitized_title) { $args = array('post_type' => 'post', 'category_name' => $sanitized_title); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php } wp_reset_postdata(); } } ?>
And the result is:
bool(true)
array(1) { [0]=> array(1) { [0]=> string(2) "14" } }
Warning: urlencode() expects parameter 1 to be string, array given in public_html/wp-includes/formatting.php on line 3690
And it outputs all posts that I have in all categories below that.
====Here is the function:
/* pmpro_getMembershipCategories() returns the categories for a given level * * $level_id is a valid membership level ID * * Return values: * Success returns boolean true. * Failure returns boolean false. */ function pmpro_getMembershipCategories($level_id) { global $wpdb; $categories = $wpdb->get_results("SELECT c.category_id FROM {$wpdb->pmpro_memberships_categories} AS c WHERE c.membership_id = '" . $level_id . "'", ARRAY_N); $returns = array(); if (is_array($categories)) { foreach ($categories as $cat) { $returns[] = $cat; } } return $returns; }
Is there anything else I can send / or do to find the solution?
Thanks for helping.
Just an FYI that we are fixing a bug in the pmpro_getMembershipCategories() function right now. I’ll post a link to the commit in a moment.
Again, so totally unrelated to what you were trying to do I noticed that pmpro_getMembershipCategories was returning an array of arrays instead of an array of category IDs. I did a Google search to see if anyone was using the function anywhere and this came up. We’re updating that in the next releases to return an array of category IDs instead.
So here is the commit/change:
https://github.com/strangerstudios/paid-memberships-pro/commit/95a9440b06b06b30a69b40c6ea2563710bdec146The new function looks like this:
function pmpro_getMembershipCategories($level_id) { global $wpdb; $categories = $wpdb->get_col("SELECT c.category_id FROM {$wpdb->pmpro_memberships_categories} AS c WHERE c.membership_id = '" . $level_id . "'"); return $categories; }
RE what you are actually trying to do, I’m honestly a little confused by the thread ?? so hoping you all can figure it out.
@strangerstudios
I’m glad we have helped to find a bug.To clarify:
According to how the plugin works, a member has to unlock his access to categories in order to get to a post. I have in each category one post, because what I really want is to have unlockable posts. (BTW the way how it works now is OK, I don’t need to change that).
However, I need to display on a member profile page all the posts he had unlocked (and links to them, I would like to be able to style them too). This means I need to get inside member’s categories and loop through them.
Now the function outputs:
array(1) {
[0]=>
string(2) “14”
}Sorry for the late reply, I’ve been away. I can’t help much anyway since I’m unfamiliar with how the membership data is organized and how it relates to posts. I hope to at least give you a direction to work towards.
It appears your function is saying a category ID of 14 is assigned. I assume assigned to the logged in user? In order to get related posts, the membership category must relate to posts in some manner. This is essential for constructing the correct query, but is not clear to me. What ever this relation is is used to join tables in the query. Then you can query for posts where conditions in the custom tables are true. Since custom tables are involved, you cannot use WP_Query very easily. Run your query using
$wpdb
methods.
- The topic ‘List posts from category name. Almost – it lists all post content as well, help.’ is closed to new replies.