Support » Fixing WordPress » get_posts($args) filters only work if the login page is opened in new tab

  • Resolved frankjackson

    (@frankjackson)


    I’m using the latest version of wordpress.

    This is the code in the sidebar. It shows a list of 6 posts. “source_domain” is a custom taxonomy. “feedweek-com,hubspot-com,getvero-com” are three values from that taxonomy. The goal (for example) is to only show posts from those three ‘source_domains’.

    It works perfectly… except under certain situations.

    This is the scenario: I’m logged in to wordpress, and viewing the site in a different browser tab. Works fine.

    Then I log out close the browser completely and then restart browse to the site’s front page … and guess what it’s not working. That filter is being ignored. It shows the 6 most recent posts regardless of which soure domain it’s assigned to.

    NOW THIS IS THE CRAZY PART:

    I then open a new browser tab and browse to wp-admin login page. Keep in mind I do NOT even log in, I just open a new tab to the login page.

    Now I flip back to the tab with the frontpage of the site and refresh the page … and crazy … the filter now works, it only shows posts from those 3 taxonomy value as it should.

    I keep repeating these steps over and over trying to get a clue, but to no avail. When ever I go to the site without first having been on a wordpress admin page (not even logged in) the filter doesn’t work. But then as soon as I open a wp admin page then the filter magically works.

    Any ideas?

    <?php // http://codex.wordpress.org/Template_Tags/get_posts ... scroll to "Taxonomy Parameters" section ?>
    <?php $args = array( 'posts_per_page' => 6, 'source_domain' => 'feedweek-com,hubspot-com,getvero-com' );
    		$myposts = get_posts($args);
    			if (empty($myposts)) { ?>
    				<p><?php _e('--- Currently Off-line ---'); ?></p>
    			<?php } else {
    				foreach ($myposts as $post) {
    					setup_postdata($post); ?>
    						<a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title() ?></a>
    	<?php }
    } ?>
    <?php wp_reset_postdata(); ?>

    [+]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Marius L. J.

    (@clorith)

    Hi,

    You’ll need to complicate the arguments a little bit 🙂

    $args = array(
    	'posts_per_page' => 6,
    	'tax_query' => array(
    		'relation' => 'OR',
    		array(
    			'taxonomy' => 'source_domain',
    			'field'    => 'slug',
    			'terms'    => array( 'feedweek-com', 'hubspot-com', 'getvero-com' )
    		)
    	)
    );

    See if the above args don’t make it behave a bit more like it should 🙂

    Thanks, I’ll try that brother, although it already fixed itself somehow after a couple of days. I guess maybe it was some weird caching thing I guess??? I’ll try your code though just to clean up my mess anyway.

    Thank you sir.

    I don’t think I should mark it solved yet though because I still have no idea what the hell was actually happening. Was it a caching issue??? I don’t know.

    Moderator Marius L. J.

    (@clorith)

    It might well have been a caching issue, it’s not uncommon to get turned around at the door by that, so to speak.

    My “complication” of your argument is to follow the new tax_query format that was introduced a few versions ago that succeeds the old method just so you’re on top of your game 🙂

    I saw that. Didn’t realize it was something new, just thought my pseudo-‘coding’ was a giant mess as normal (normally a cobbled mix of modified scraps of snippits copied and pasted from elsewere).

    I’ll definitely change it now though thanx.

    BTW, while I’ve got your attn: (if not trying to monopolize your good deeds), do you know anything about htaccess??? >>> 🙂

    https://wordpress.org/support/topic/htaccess-vary-accept-encoding-not-being-found-by-toolspingdomcom?replies=3#post-6446615

    Thanks in advance if…

    [+]

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_posts($args) filters only work if the login page is opened in new tab’ is closed to new replies.