• Resolved naminta

    (@naminta)


    Hey there,

    for my current project i need some help or a kind of advice:

    I am using the plugin cimy user extra fields to give my subscribers the chance to put some content of there choice to there profile.

    Next I would like to take this information (more than one field) and return on an extra site posts to the topics of the users choice, for each topic 3 – 5.

    I wrote some php to get my dates based of the current users id from the database, all fine, working.

    But I can’t find any function to search posts with it now and i don’t want to use search form, it has to be an extra site or something like that, maybe a sidewidget, something like google does for you if you have searched for a topic and it gives you more results.

    Is that possible? And if it is could it be arranged by wordpress attributes (most clicked, most comments, newest, etc.).

    Hope someone could help me here. Sorry for my bad english.

    • This topic was modified 8 years, 1 month ago by naminta.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the WP_Query class (or get_posts(), which uses this class) and assign your search terms to the “s” argument. Then run a loop as shown in the examples to display the results.

    You can order results by any available orderby argument. Ordering by anything else requires either a custom query or post query ordering in PHP, such as with usort().

    Thread Starter naminta

    (@naminta)

    Sounds like what i was searching for, will try this and give you a feedback ??

    Maybe if i code like an idiot i will write before finishing ^^
    but first i will try it by myself ??

    thx so far

    Thread Starter naminta

    (@naminta)

    Hey @bcworkz,

    while coding there was one more thing i would like to ask, is it possible to use an array here for keyword:

    $query = new WP_Query( array( 's' => 'keyword' ) );

    otherwise i would build a loot around and try it this way.

    Thread Starter naminta

    (@naminta)

    not the super php dude

    <?php
    
    function get_special_user_content (){
    	
    	$userid = wp_get_current_user();	
    	$userfields = array(get_cimyFieldValue($userid, 'PFLEGEGRAD'), get_cimyFieldValue($userid, 'BEHINDERUNGSGRAD'), get_cimyFieldValue($userid, 'ORT'), get_cimyFieldValue($userid, 'PLZ'));
    	
    	//The Loop to run arraypositons
    	for ($i = 0; $i < count($userfields); $i++){
    		
    		//The Querry argument (for every userfield new)
    		$query = new WP_Query( array( 's' => $userfields[i] ) );
    	
    		// The Loop to display content
    		if ( $query->have_posts() ) {
    			echo '<ul>';
    			while ( $query->have_posts() ) {
    				$query->the_post();
    				echo '<li>' . get_the_title() . '</li>';
    			}
    			echo '</ul>';
    			/* Restore original Post Data */
    			wp_reset_postdata();
    		}
    		else {
    			echo '<p>Keine Beitr?ge für Ihre Benutzerangabe ' . $array[i] . 'gefunden</p>';
    		}
    	}
    }
    
    ?>

    What you think, could that work?
    And i am still new in using wordpress, maybe someone have an advise how i can get my snippet on an page and into the CMS itself.

    I would suggest using the PlugIn My Custom Functions to add the code to the functions.php. With the PlugIn Insert PHP I would display the function on a page.

    Any easier ways?

    EDIT: Tested the array with var_dump();, works, other code is not working like i would wish it should

    • This reply was modified 8 years, 1 month ago by naminta. Reason: Test on my array
    Moderator bcworkz

    (@bcworkz)

    The ‘s’ argument does not accept arrays, but you can pass multiple terms. 's' => 'foo bar -snaf' will return posts with ‘foo’ or ‘bar’ in the title or content, but not posts that also have ‘snaf’. If you want a match to all terms, an AND query instead of OR, a custom query is required.

    I don’t know what ends up in $userfields, but one problem with your approach is you can end up with redundant results. The same post can appear in every list if it contains all the terms. I’d suggest concatenating all the terms into a single string with terms separated by spaces. You can use implode() for this. Then you’ll get all the results at once without redundant posts.

    Instead of adding more plugins (which is fine if that’s what you like), I prefer to work with minimal or no extra plugins. I place my custom code in either a child theme or a site specific plugin solely for all of my custom code. I display results of custom code either by creating a custom page template or by making a custom shortcode. The shortcode can be added to any content and it’s output replaces the shortcode anytime someone views the post content.

    Thread Starter naminta

    (@naminta)

    hey solved it myself, for everyone who is intressted here is the mainpart of my code

    @bcworkz ‘sS agrgument worked with an array for me ??
    don’t why, but it does, $userfields is still an array, now it is just simple html to get the users the content shown as i want it to look =>

    thank you very much for your help

    CODE:

    $userid = wp_get_current_user();
    
    //Array for the s argument
    $userfields = array(get_cimyFieldValue($userid, 'field1'), get_cimyFieldValue($userid, 'field2'), get_cimyFieldValue($userid, 'fieldn'));
    
    //Testprint if you have new arguments to look for
    //var_dump($userfields);
    
    //Query
    $query = new WP_Query( array( 's' => $userfields ) );
    
    //Loop
    if ( $query->have_posts() ) {
    	echo '<ul style="text-align: center;">';
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		//No Pages!
    		if (get_post_type() == 'page'){
    			//do nothing
    		}
    		else{
    			echo '<li>';
    			echo "<a href='" . get_permalink() . "'>" . get_the_title() . " | Post date: " . get_the_date( 'd-m-Y' ) . "</a>";
    			echo '</li>';
    		}
    	}
    	echo '</ul>';
    	/* Restore original Post Data */
    	wp_reset_postdata();
    }
    else {
    	echo '<p>No Posts for the Topic ' . $array[i] . '</p>';
    }
    Thread Starter naminta

    (@naminta)

    @bcworkz, you were right, array did not worked that way, made an rework today, just if someone wants to use this, here is the reworked Maincode:

    <?php
    $userid = wp_get_current_user();
    
    //Array for the s argument
    $userfields = array(get_cimyFieldValue($userid, 'field1'), get_cimyFieldValue($userid, 'field2'), get_cimyFieldValue($userid, 'fieldn'));
    
    //Testprint for the array
    //var_dump($userfields);
    
    //for-loop to run the array
    for ($i=0; $i<count($userfields); $i++){
    
    //Query + headline for the argument on position i
    echo '<h4>Posts for the user field ' . $userfields[$i] . '</h4>';
    $query = new WP_Query( array( 's' => $userfields[$i] ) );
    
    if ( $query->have_posts() ) {
    	echo '<ul>';
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		//Only if no Page
    		if (get_post_type() == 'page'){
    			//do nothing
    		}
    		else{
    			echo '<li>';
    			echo "<a href='" . get_permalink() . "'>" . get_the_title() . " | Post date: " . get_the_date( 'd-m-Y' ) . "</a>";
    			echo '</li>';
    		}
    	}
    	echo '</ul>';
    	/* Restore original Post Data */
    	wp_reset_postdata();
    }
    else {
    	echo '<p>No Posts for the Topic ' . $userfields[$i] . '.</p>';
    }
    }
    ?>

    Now it is solved, to get the array dynamic filled you could work with a switch case or if else and check for empty variables, could look like this:

    <?php
    $userid = wp_get_current_user();
    
    $field1 = '';
    $field2 = '';
    $field3 = '';
    
    $userfield = array();
    
    if ($field1 != '' && $field2 != '' §§ field3 != ''){
    $userfields = array(get_cimyFieldValue($userid, 'field1'), get_cimyFieldValue($userid, 'field2'), get_cimyFieldValue($userid, 'field3'));
    }
    ifelse (...........and so on ....
    
    • This reply was modified 8 years ago by naminta.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Content based on User Profile Fields’ is closed to new replies.