I am using 'Now Reading' plugin in a wordpress project. In plugin's sidebar template I am using this query:
while( have_books('status=read&orderby=finished&num=2') ) : the_book();
to select 2 books.
What parameter should I pass to make it random? I tried with 'order=rand' and 'rand=true' but it did not work.
Any help will be appreciated!
Thanks in advance..
A random function doesn't actually exist. I have used this code, in my themes functions.php to allow random order before - not sure if it'll work in this situation, but worth a try.
Add this to your themes functions.php file:
function query_random_posts($query) {
return query_posts($query . '&random=true');
}
class RandomPosts {
function orderby($orderby) {
if ( get_query_var('random') == 'true' )
return "RAND()";
else
return $orderby;
}
function register_query_var($vars) {
$vars[] = 'random';
return $vars;
}
}
add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
Then try this in your sidebar file:
while( have_books('status=read&orderby=finished&num=2&random=true') ) : the_book();
If not, my only other suggestion would be to get the 10 latest books, add them all to a new array, and then shuffle that array. May be a bit bloated though.
Related
I appreciate questions similar to this have been asked before but the answers I've tried aren't doing what I need.
Basically,
I have this file in the woocommerce plugin folder structure:
wp-content\plugins\woocommerce\includes\wc-coupon-functions.php
Inside the file is the following function:
function wc_get_cart_coupon_types() {
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart') );
}
I need to override it so it returns an additional item in the array but nothing I've tried has worked. I've tried:
Creating the same file in my custom theme file
hooking the function in my functions file with the following code:
function woocommerce_coupon_get_cart_coupon_types()
{
return (array) apply_filters( 'woocommerce_cart_coupon_types', array( 'fixed_cart', 'custom_discount' ) );
}
add_filter('wc_get_cart_coupon_types', 'woocommerce_coupon_get_cart_coupon_types',10, 1);
Any other suggestions would be greatly appreciated, also..... I've made the change directly in the file and it definitely works. Thanks
Your #2 approach is sort of how to do it, but you're essentially caught in a loop situation the way you did it.
You need to do it this way:
function wpso59974749_woocommerce_coupon_get_cart_coupon_types( $data ) {
$data[ 'your_new_key_here' ] = 'your new value here';
return $data;
}
add_filter('woocommerce_cart_coupon_types','wpso59974749_woocommerce_coupon_get_cart_coupon_types',10, 1);
You shouldn't add the apply_filter back in your function, as it would get stuck in a loop - essentially refiltering itself over and over.
I prefixed your function so if there is another woocommerce_coupon_get_cart_coupon_types function, it won't conflict.
I am trying to add a tracking mechanism to my wordpress plugin. And I want to use the WP cron mechanism. So I have an options page and when users save all options I want to use a hook to remove or add the tracking to the wp cron depending of the admins choice.
But right now I am stuck.
I have:
register_setting ( 'my-settings-group', 'myplugin_tracking');
add_action ( 'update_option_myplugin_tracking', 'myplugin_schedule_tracking' );
function myplugin_schedule_tracking($old_value, $new_value)
{
echo "Setting is updated!";
echo $old_value;
}
But this does not seem to work. I also used:
add_filter ( 'update_option_myplugin_tracking', 'myplugin_schedule_tracking' );
The option is saved in a form that posts to the options.php if that matters.
What am I doing wrong? Hope somebody can help out as I cannot find much information about doing something upon updating an option!
Thank you.
Okay this seems to work after all.
register_setting ( 'my-settings-group', 'myplugin_tracking');
function myplugin_schedule_tracking($old_value, $new_value)
{
if ($old_value !== $new_value)
{
if ($new_value == '')
{
wp_clear_scheduled_hook( 'myplugin_tracking' );
}
elseif ($new_value == 'on' && $current_schedule == FALSE)
{
wp_schedule_event( time(), 'hourly', 'myplugin_tracking' );
}
}
}
add_filter ( 'update_option_myplugin_tracking', 'myplugin_schedule_tracking', 10, 2);
add_action ( 'myplugin_tracking', 'myplugin_tracking' );
In the function myplugin_tracking you do whatever you have to do to track.
My problem was that I did not see the echo on the screen but it did appear to work after all.
Perhaps not the best code but it may be helpful for others :-)
I have a problem I've encountered when trying to finish a project.
I have the current permalink structure set as /%postname%/
I made my own function on giving a prefix to posts only so my posts are rewritten as /{prefix}/%postname%/.
My problem is that I want to change the permalink of the pages as I did with the posts so my pages will have a prefix like /{prefix}/%pagename%/.
What I tried and didn't work:
Re-declare the PAGES post type and set a rewrite slug.
Tried adding a custom rewrite rule as a function but it didn't work:
$rewrite_rules += array('mycustomprefix/(.+?)/([0-9]+)/([^/]+)/([^/]+)/?$' =>'index.php?pagename=$matches[1]',
Is this possible? Are there any developers out there who encountered the same issue?
For anybody interested, I've fixed my issue in the following manner:
function change_author_permalinks() {
global $wp_rewrite;
// Change the value of the author permalink base to whatever you want here
$wp_rewrite->author_base = '';
// Change the value of the page permalink base to whatever you want here
$wp_rewrite->page_structure = 'static/%pagename%';
$wp_rewrite->flush_rules();
}
add_action('init','change_author_permalinks');
Hope this helps others as I couldn't find any help for this anywhere. For morer information on what you can change this way, check out http://codex.wordpress.org/Class_Reference/WP_Rewrite
Have you updated the permalinks structure after adding this rewrite to your functions.php ? It works for me :)
add_filter( 'page_rewrite_rules', 'customprefix_page_rewrite_rules' );
function customprefix_page_rewrite_rules( $rewrite_rules )
{
end( $rewrite_rules );
$last_pattern = key( $rewrite_rules );
$last_replacement = array_pop( $rewrite_rules );
$rewrite_rules += array(
'mycustomprefix/(.+?)/?$' => 'index.php?pagename=$matches[1]',
$last_pattern => $last_replacement,
);
return $rewrite_rules;
}
I found this solution working for me better... and it's also much cleaner code.
add_action( 'init', 'custom_page_rules' );
function custom_page_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . 'your-page-prefix/%pagename%';
}
I found the code here: http://wpforce.com/change-wordpress-page-permalinks/
i'm currently working on a wordpress site that needs to have the option to be offered in french. i've found a way to make the theme work with the fr_FR po and mo files when i add a querystring variable l. i.e.
site.tld will yield the vanilla english site, while site.tld/?l=fr will activate the following code in my functions.php to serve the french translation:
<?php
// http://codex.wordpress.org/Plugin_API/Filter_Reference/locale
add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
if ("fr" == substr(strtolower(trim(strip_tags(stripslashes($_GET['l'])))), 0, 2)) {
// set language to french
return "fr_FR";
} else {
// return original locale
return $lang;
}
}
?>
this setup is already working. my question is: how do i rewrite the url so instead of site.tld/?l=fr i can just prepend the folder structure with fr i.e. site.tld/fr/?
so like if there's a page site.tld/portoflio/autumn/ in english, site.tld/fr/portfolio/autumn/ will spit out the french one. i got this idea from the apple website, where the language is always prepended to the folder structure.
i can already make this happen with an external redirect in htaccess:
RewriteBase /
RewriteCond ^[a-z]{2}/
RewriteRule ^([a-z]{2})/(.*)$ /$2?l=$1 [R,L]
this works, but once i remove the R flag it serves the french-translated 404 not found isntead. i'm guessing what i did is messing-up with wordpress' rewrite rules because i need to use pretty permalinks. right now i'm set to use Month and name (/%year%/%monthnum%/%postname%/) in the admin general options.
my question is, how do i make wordpress ignore the /fr/ part and still serve the correct page/post/etc?
is this even possible? am i on the right track here? i need your help especially doing what's WISE and not just what's NICE. i tried this http://pmg.co/a-mostly-complete-guide-to-the-wordpress-rewrite-api but for the life of me i can't make it work. :/
UPDATE: ok, so here's some progress taking cue from #relu below:
i made sure my rules were canned from .htaccess
i then added the rules from #relu but in the following way, because Relu's always sent me to the same address without the /fr:
<?
// http://pmg.co/custom-wordpress-shortlinks
add_action( 'init', 'pmgtut_add_rewrites' );
function pmgtut_add_rewrites() {
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
add_rewrite_rule( '^([a-z]{2})/(.*)$', 'index.php?l=$matches[1]&q=$matches[2]', 'top' );
}
add_filter( 'query_vars', 'pmgtut_query_vars', 10, 1 );
function pmgtut_query_vars( $vars ) {
$vars[] = 'l';
return $vars;
}
?>
now the /fr stays in the address bar, so that's a good thing, but two problems persist:
wordpress is serving me the main index page. seems like it's not using the &q=$matches[2] part of the rule; and
the locale still doesn't get set properly. i checked if the variable l is getting fed, so i added echo 'l: $l'; after $l = get_query_var('l'); and interestingly i get two echoes: one l: and another l: fr right after that. is the locale filter being run twice? seems like the first time it doesn't see the value of the queryvar, and then there seems to be a second time that it outputs l: with fr already in there.. at the end of the day the locale still doesn't get changed.
aaaah help.
FINAL UPDATE: in my last breath of frustration i searched again, and found this plugin qtranslate. does what i need. thanks y'all, esp to #relu's epic effort and stayin pow'r.
Add this to your functions.php
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['^([a-z]{2})/(.*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['^([a-z]{2})/(.*)$'] = 'index.php?l=$matches[1]&q=$matches[2]';
return $newrules + $rules;
}
// Adding the l var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'l');
return $vars;
}
Haven't tested this, it may need some tweaking, but this is the way to do it.
I've basically adapted the version found here.
It also registers the "l" query var so you can get it's value by calling: get_query_var
add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
$l = get_query_var('l');
if ("fr" == $l) {
// set language to french
return "fr_FR";
} else {
// return original locale
return $lang;
}
}
UDATE: Ignore the above!
Remove the my_insert_query_vars() filter and replace it with:
function register_rewrite_tag() {
add_rewrite_tag('%l%', '([a-z]{2})');
}
add_action('init', 'register_rewrite_tag');
Here are also the updated rewrites for pages and posts, I've looked at WordPress' $wp_rewrite->rules and adapted them to fit this special case:
function pmgtut_add_rewrites() {
add_rewrite_rule( '^([a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
add_rewrite_rule( '^([a-z]{2})/(.?.+?)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&pagename=$matches[2]&page=$matches[3]', 'top' ); // Pages
add_rewrite_rule( '^([a-z]{2})/([^/]+)(/[0-9]+)?/?$', 'index.php?l=$matches[1]&name=$matches[2]&page=$matches[3]', 'top' ); // Posts
}
maybe out of topic but here's another rewrite rule for languages of the form; en_us/ or jp/
add_rewrite_rule( '^([a-z]{2}_[A-Z]{2}|^[a-z]{2})/?$', 'index.php?l=$matches[1]', 'top' );
I know that you can generate a feed using urls like: ?cat=3&feed=rss2
And you can switch it around to exclude the category 3 by putting a subtract sign in front: ?cat=-3&feed=rss2
But, it doesn't look like you can do the same for posts? I'm using the JW video Player and have loaded the related plugin. The related plugin can take an rss-feed (media rss) as the parameter so it can link to other videos/wordpress posts that are related.
My problem is that currently this means that the active video also appears in the related videos feed.
What would be the best solution for solving this problem? I aim to create my own rss feed generator in the future, but for now I just want to keep it simple and use the generated feeds that wordpress creates. Is there a simple way to add support for an url parameter named post for example? It could then take post=-7 to exclude post with id 7 from displaying in the feed.
Or is there better solutions for this?
You can use a function
function exclude_category($query){
if ( $query->is_home || $query->is_feed || $query->is_archive ) {
$query->set('cat', '-1');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
see'a
Instead of explaining the mechanisem - there are many plugins just for that ..
One that I know is :
StelthPubish
Edit I
I do not know about the URL - but you can try use the
function ok9_feed_filter($query) {
if ( !$query->is_admin && $query->is_feed) {
$query->set('post__not_in', array(15, 6) ); // page /post id
}
return $query;
}
add_filter( 'pre_get_posts', 'ok9_exclude_filter' );
or this
function ok9_feed_exlude($where, $wp_query = NULL) {
global $wpdb;
if ( !$wp_query )
global $wp_query;
if ($wp_query->is_feed) {
// exclude post id
$where .= " AND $wpdb->posts.ID NOT IN (15, 6)";
}
return $where;
}
add_filter( 'posts_where','ok9_feed_exlude', 1, 2 );
If you do not want to use a fixed id in the function - you can always add a custom field in the posts you want to exclude , and use that in the query ..