Only show posts from selected post categories - wordpress

I’m building a news aggregator site where I fetch news from a couple of different news sources that converts into Posts. I want my registered users to have the possibility to choose which news sources (Post Categories) they want to see in their feed on my website (not BuddyPress own Activity Feed, but the feed where I list all mixed news posts from different sources).
Is this possible with a custom field or something? So that when the user edits their profile, they can tick the box next to the source they want to add/delete form their feed.
An example of what I want each user to be able to have the freedom to do:
“Select which news sources (Post Categories) you want to read from:”
[ ] USA Today
[ ] The Guardian
[✓] The New Yorker
The specific user above should now only see news from The New Yorker in the news feed.
How would you guys accomplish a solution like this with BuddyPress?
Update
Clarification:
Emma logs onto her account (via BuddyPress plugin) and goes to her Account Settings. In the Account Settings page, I want to list a couple of Checkboxes with news sources. If Emma selects two of the Checkboxes (i.e. The Guardian & USA Today), then Emma wants the WP_Query to only select posts from these Post Categories (which is the different newspapers).
1. How should I create these fields using Advanced Custom Fields and present them on the Profile Settings page?
2. How should I take the values selected from the Advanced Custom Fields in Profile Settings, and filter the WP_Query (which fetches all posts) by Post Category?

Try the Advanced Custom Fields plugin.
http://www.advancedcustomfields.com/
You can use the Taxonomy field type, and use this to get the selected values.
http://www.advancedcustomfields.com/resources/code-examples/#working-with array values
$values = get_field('field_name');
if($values)
{
echo '<ul>';
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
echo '</ul>';
}
Then just use a custom query and filter by the category IDs.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$query = new WP_Query( 'cat=2,6,17,38' );

I have made this work, take the input on selection of categories and save the ids of selected categories in custom table and query saved ids ..

Related

Replace Blog Post Author link with Team Member link (CPT + ACF)

I'm building a website that has "Team Members" section and "Blog" section. Team members are a custom post type.
The client requested that the links to authors in blog posts should point to associated Team Member page instead of the default author page in WordPress (basically they don't want to have default author pages at all).
I've found this solution offered by #Damocles - Use "Team Members" custom post type instead of Author for blog posts
Basically the solution proposed by him was simple and exactly as I thought initially of tackling this issue:
Create a "Post Object" ACF field and set it to filter through "Team Member" custom post types
Attach this field to User accounts
Go to user profile and choose the correct Team Member from drop down menu
Then use a filter in functions.php to automatically replace the author link everywhere with associated Team Member url
Makes sense but unfortunately, it doesn't want to work on my website. I even used the same name for ACF field as he did and used the exact same code in functions.php:
add_filter( 'author_link', 'team_author_link', 10, 3 );
function team_author_link( $link, $author_id, $author_nicename ) {
$team_post_id = get_field('team_post', $author_id);
// if the team post is set, get the permalink to the team post:
$team_link = get_permalink($team_post_id);
$link = ($team_link !== false) ? $team_link : $link;
return $link;
}
The author link DOES change, BUT instead of pointing to the associated Team Member page, all author links point to the currently opened blog post URL. I don't know, maybe my theme is overwriting the query or something, so the URL to the custom post type cannot be achieved from blog post view?
Can someone help me achieve it, please? I want to attach a Team Member (custom post type) to user account in WordPress and replace the author link through functions.php to the associated Team Member page url.
It's because you are missing a prefix, to tell ACF that it should be looking on a user.. - try this
$team_post_id = get_field('team_post', 'user_'.$author_id);
Docs are here : https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

Combine Wordpress site search results with product search results on one page

On my Wordpress website, a site search that is limited to only posts renders a perfect results display of posts. A product search also renders a perfect display of products, although displayed differently. But if I attempt to search for both posts and products, the display is terrible. I think the products are probably forced into the posts display template. Since each of my products includes an explanation post inside the product description, those posts are repeated after every product -- mighty ugly. I don't need any product descriptions to be shown the results and prefer all products to be displayed like they are in the only product search.
This is all visible if you search for 'Grace' using the three different search inputs at this testing post: https://www.hebrewwordpics.com/dummypost/
I want to use only one search input form that will do both searches and display them together sequentially on one page.
Using WooCommerce and StoreFront child theme. Currently limiting searches with Ivory Search plugin, but the results are similar with other plugins or none at all.
You can use get_post_type() to identify which post type is being queried. Then display a specific template per post type.
get_post_type( int|WP_Post|null $post = null )
Retrieves the post type of the current post or of a given post.
Parameter
Description
$post
(int/WP_Post/null) (Optional) Post ID or post object. Default is global $post. Default value: null
You can use the following on your search page.
<?php
if ( 'post' === get_post_type() ):
// ... post template
elseif ( 'product' === get_post_type() ):
// ... product template
else:
// ... else
endif; ?>

Wordpress - remove some posts from query

I'm trying to make a plugin.
Its job is to generate and send a link to the author after a post is published by admin.
After clicking on the link will be the post actually published.
I did that after the click on the link there will be a post meta added to the post.
NOW I cant find a solution how to show only posts with the meta or ADMINS (or with some user level) posts.
I decided I need a filter bud I cant figure out how to do the ADMIN posts exceptions.
How do I filter only non-admin posts.
I think I need to remove the "bad" posts from $query but how ?
add_filter( 'pre_get_posts' , 'postsClean' );
function postsClean( $query ){
// check all posts and if the post should be not published remove it from query
}
Or is there any better way ?
If you are using wp_query you can use - for negation, e.g.
$wp_query_obj->set( 'author', '-1' );
WP_Query shows a full list of query arguments.

Allow only certains user posts to display on a page in Wordpress?

I have two special users on my site. Let's call them user1 and user2. User 1 manages "user page 1" and user2 manages "user page 2". I want their posts to appear when I click on these links and only see their posts in Wordpress.
Should I use categories for this and if so, how do I create a link to only show posts from a certain category?
Do I need a module for this?
If the answer is still not clear, how do I do this?
You can use wp_qurey() function to show posts from specific authors.
Display posts by author, using author id:
$query = new WP_Query( 'author=123' );
Display posts by author, using author 'user_nicename':
$query = new WP_Query( 'author_name=rami' );
More details here - http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
You can use the get_author_posts_url() to create a link to a page listing a specific author's posts: http://codex.wordpress.org/Function_Reference/get_author_posts_url
The format of the link (if you just want to go ahead and test it in your browser first) will be http://www.yoursite.com/author/authorname.
Hope that helps!
Copy your index page loop and then change query from post_per_page query to this and you're done.
$query = new WP_Query( 'author_name=rami' );

Syndicating custom fields in Wordpress via RSS

I wonder if I could ask a Wordpress / RSS question I could't find an answer for around here,
Trying to syndicate posts via RSS in Wordpress using the FeedWordpress plugin as an RSS aggregator, each post in the original blog includes five custom fields that are important for its Theme functionality (the original and syndicating / receiving blog using the same theme).
The original RSS2 feed doesn't include these custom fields apart from one, being enclosure, that is defined in the default rss feed template (function in WP rss_enclosure).
This is written in the original feed such as:
<enclosure url="http://www.samplevideourl.flv" length="18554755" type="video/x-flv" />
Tried to add the rest of the custom fields modifying the rss2-feed.php template so they show at the end of each segment in the current RSS2 feed, now they are included as for example:
...
<ratings_average>0</ratings_average>
<views>5</views>
</item>
However, if I update the syndicated posts, or delete the posts and fetch the modified feed again with feedwordpress, none of these show in the syndicated posts.
Is there a way to include these custom fields so they are recognized by feedwordpress?
Basically need to syndicate the same format of the post as the original including all its custom fields.
Many Thanks
Carlos
There is a thread that covers this: https://wordpress.stackexchange.com/questions/3801/add-custom-fields-to-custom-post-type-rss
I've condensed the answers there to reflect the later improvements (thanks MikeSchinkel, prettyboymp and Acts7).
Add this to your theme's functions.php:
/* IN ORDER TO VALIDATE you must add namespace */
add_action('rss2_ns', 'my_rss2_ns');
function my_rss2_ns(){
echo 'xmlns:mycustomfields="'. get_bloginfo('wpurl').'"'."\n";
}
add_action('rss2_item', 'yoursite_rss2_item');
function yoursite_rss2_item() {
if (get_post_type()=='my_custom_post_type') {
$fields = array( 'field1', 'field2', 'field3' );
$post_id = get_the_ID();
foreach($fields as $field)
if ($value = get_post_meta($post_id,$field,true))
echo "<mycustomfields:{$field}>{$value}</mycustomfields:{$field}>\n";
}
}
This will add all custom field names and values to the site's main feed.
Note, for custom fields with more than one value, a modification is necessary as the above will only work for single value fields, not arrays.
So,
On your Master site (where you are syndicating FROM) you add the above function.
On the Slave site (where you are syndicating TO), assuming you have FeedWordPress installed, go to "SYNDICATION" ->
Click on the name of the RSS feed
Go to Custom Feed Settings and plug in the pieces

Resources